Merge xmrig v6.7.0 into master

This commit is contained in:
MoneroOcean 2020-12-23 06:03:02 +00:00
commit 1719879f7e
249 changed files with 6814 additions and 6134 deletions

View file

@ -34,8 +34,8 @@
#include "base/io/Env.h"
#include "base/io/json/Json.h"
#include "base/kernel/Base.h"
#include "base/tools/Buffer.h"
#include "base/tools/Chrono.h"
#include "base/tools/Cvt.h"
#include "core/config/Config.h"
#include "core/Controller.h"
#include "version.h"
@ -169,9 +169,6 @@ void xmrig::Api::exec(IApiRequest &request)
# ifdef XMRIG_FEATURE_HTTP
features.PushBack("http", allocator);
# endif
# ifdef XMRIG_FEATURE_LIBCPUID
features.PushBack("cpuid", allocator);
# endif
# ifdef XMRIG_FEATURE_HWLOC
features.PushBack("hwloc", allocator);
# endif
@ -222,13 +219,13 @@ void xmrig::Api::genId(const String &id)
const size_t inSize = (sizeof(APP_KIND) - 1) + addrSize + sizeof(uint16_t);
const auto port = static_cast<uint16_t>(m_base->config()->http().port());
auto*input = new uint8_t[inSize]();
auto *input = new uint8_t[inSize]();
memcpy(input, &port, sizeof(uint16_t));
memcpy(input + sizeof(uint16_t), interfaces[i].phys_addr, addrSize);
memcpy(input + sizeof(uint16_t) + addrSize, APP_KIND, (sizeof(APP_KIND) - 1));
keccak(input, inSize, hash);
Buffer::toHex(hash, 8, m_id);
Cvt::toHex(m_id, sizeof(m_id), hash, 8);
delete [] input;
break;

View file

@ -22,6 +22,7 @@ set(HEADERS_BASE
src/base/kernel/config/BaseConfig.h
src/base/kernel/config/BaseTransform.h
src/base/kernel/config/Title.h
src/base/kernel/constants.h
src/base/kernel/Entry.h
src/base/kernel/interfaces/IAsyncListener.h
src/base/kernel/interfaces/IBaseListener.h
@ -66,6 +67,7 @@ set(HEADERS_BASE
src/base/tools/Baton.h
src/base/tools/Buffer.h
src/base/tools/Chrono.h
src/base/tools/Cvt.h
src/base/tools/Handle.h
src/base/tools/String.h
src/base/tools/Timer.h
@ -113,7 +115,7 @@ set(SOURCES_BASE
src/base/net/tools/LineReader.cpp
src/base/net/tools/NetBuffer.cpp
src/base/tools/Arguments.cpp
src/base/tools/Buffer.cpp
src/base/tools/Cvt.cpp
src/base/tools/String.cpp
src/base/tools/Timer.cpp
)
@ -230,13 +232,6 @@ if (WITH_KAWPOW)
)
endif()
if (WITH_PROFILING)
add_definitions(/DXMRIG_FEATURE_PROFILING)
list(APPEND HEADERS_BASE src/base/tools/Profiler.h)
list(APPEND SOURCES_BASE src/base/tools/Profiler.cpp)
endif()
if (WITH_RANDOMX AND WITH_BENCHMARK)
add_definitions(/DXMRIG_FEATURE_BENCHMARK)

View file

@ -168,7 +168,7 @@ typedef uint64_t state_t[25];
void xmrig::keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen)
{
state_t st;
uint8_t temp[144];
alignas(8) uint8_t temp[144];
int i, rsiz, rsizw;
rsiz = sizeof(state_t) == mdlen ? HASH_DATA_AREA : 200 - 2 * mdlen;

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -50,20 +44,9 @@ xmrig::Console::Console(IConsoleListener *listener)
xmrig::Console::~Console()
{
stop();
}
void xmrig::Console::stop()
{
if (!m_tty) {
return;
}
uv_tty_reset_mode();
Handle::close(m_tty);
m_tty = nullptr;
}

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -29,7 +23,16 @@
#include "base/tools/Object.h"
#include <uv.h>
using uv_buf_t = struct uv_buf_t;
using uv_handle_t = struct uv_handle_s;
using uv_stream_t = struct uv_stream_s;
using uv_tty_t = struct uv_tty_s;
#ifdef XMRIG_OS_WIN
using ssize_t = intptr_t;
#else
# include <sys/types.h>
#endif
namespace xmrig {
@ -46,8 +49,6 @@ public:
Console(IConsoleListener *listener);
~Console();
void stop();
private:
bool isSupported() const;

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -23,23 +17,30 @@
*/
#include <uv.h>
#include "base/io/Signals.h"
#include "base/kernel/interfaces/ISignalListener.h"
#include "base/io/log/Log.h"
#include "base/io/log/Tags.h"
#include "base/io/Signals.h"
#include "base/tools/Handle.h"
#ifdef SIGUSR1
static const int signums[xmrig::Signals::kSignalsCount] = { SIGHUP, SIGINT, SIGTERM, SIGUSR1 };
#else
static const int signums[xmrig::Signals::kSignalsCount] = { SIGHUP, SIGINT, SIGTERM };
#endif
xmrig::Signals::Signals(ISignalListener *listener)
: m_listener(listener)
{
# ifndef XMRIG_OS_WIN
signal(SIGPIPE, SIG_IGN);
# endif
for (size_t i = 0; i < kSignalsCount; ++i) {
uv_signal_t *signal = new uv_signal_t;
signal->data = this;
auto signal = new uv_signal_t;
signal->data = this;
m_signals[i] = signal;
@ -51,24 +52,37 @@ xmrig::Signals::Signals(ISignalListener *listener)
xmrig::Signals::~Signals()
{
stop();
}
void xmrig::Signals::stop()
{
if (!m_signals[0]) {
return;
}
for (size_t i = 0; i < kSignalsCount; ++i) {
Handle::close(m_signals[i]);
m_signals[i] = nullptr;
for (auto signal : m_signals) {
Handle::close(signal);
}
}
void xmrig::Signals::onSignal(uv_signal_t *handle, int signum)
{
switch (signum)
{
case SIGHUP:
LOG_WARN("%s " YELLOW("SIGHUP received, exiting"), Tags::signal());
break;
case SIGTERM:
LOG_WARN("%s " YELLOW("SIGTERM received, exiting"), Tags::signal());
break;
case SIGINT:
LOG_WARN("%s " YELLOW("SIGINT received, exiting"), Tags::signal());
break;
# ifdef SIGUSR1
case SIGUSR1:
LOG_V5("%s " WHITE_BOLD("SIGUSR1 received"), Tags::signal());
break;
# endif
default:
break;
}
static_cast<Signals *>(handle->data)->m_listener->onSignal(signum);
}

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -26,10 +20,14 @@
#define XMRIG_SIGNALS_H
#include <stddef.h>
#include "base/tools/Object.h"
typedef struct uv_signal_s uv_signal_t;
#include <csignal>
#include <cstddef>
using uv_signal_t = struct uv_signal_s;
namespace xmrig {
@ -41,20 +39,24 @@ class ISignalListener;
class Signals
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Signals)
# ifdef SIGUSR1
constexpr static const size_t kSignalsCount = 4;
# else
constexpr static const size_t kSignalsCount = 3;
# endif
Signals(ISignalListener *listener);
~Signals();
void stop();
private:
void close(int signum);
static void onSignal(uv_signal_t *handle, int signum);
ISignalListener *m_listener;
uv_signal_t *m_signals[kSignalsCount];
uv_signal_t *m_signals[kSignalsCount]{};
};

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -29,6 +23,7 @@
#include <cassert>
#include <cmath>
#include <istream>
namespace xmrig {
@ -119,6 +114,21 @@ const rapidjson::Value &xmrig::Json::getValue(const rapidjson::Value &obj, const
}
double xmrig::Json::getDouble(const rapidjson::Value &obj, const char *key, double defaultValue)
{
if (isEmpty(obj)) {
return defaultValue;
}
auto i = obj.FindMember(key);
if (i != obj.MemberEnd() && (i->value.IsDouble() || i->value.IsLosslessDouble())) {
return i->value.GetDouble();
}
return defaultValue;
}
int xmrig::Json::getInt(const rapidjson::Value &obj, const char *key, int defaultValue)
{
if (isEmpty(obj)) {
@ -149,6 +159,25 @@ int64_t xmrig::Json::getInt64(const rapidjson::Value &obj, const char *key, int6
}
xmrig::String xmrig::Json::getString(const rapidjson::Value &obj, const char *key, size_t maxSize)
{
if (isEmpty(obj)) {
return {};
}
auto i = obj.FindMember(key);
if (i == obj.MemberEnd() || !i->value.IsString()) {
return {};
}
if (maxSize == 0 || i->value.GetStringLength() <= maxSize) {
return i->value.GetString();
}
return { i->value.GetString(), maxSize };
}
uint64_t xmrig::Json::getUint64(const rapidjson::Value &obj, const char *key, uint64_t defaultValue)
{
if (isEmpty(obj)) {
@ -222,6 +251,11 @@ bool xmrig::Json::convertOffset(std::istream &ifs, size_t offset, size_t &line,
}
xmrig::JsonReader::JsonReader() :
m_obj(kNullValue)
{}
bool xmrig::JsonReader::isEmpty() const
{
return Json::isEmpty(m_obj);

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -26,13 +20,10 @@
#define XMRIG_JSON_H
#include "3rdparty/rapidjson/fwd.h"
#include "base/kernel/interfaces/IJsonReader.h"
#include <string>
#include <vector>
#include <fstream>
namespace xmrig {
@ -47,8 +38,10 @@ public:
static const rapidjson::Value &getArray(const rapidjson::Value &obj, const char *key);
static const rapidjson::Value &getObject(const rapidjson::Value &obj, const char *key);
static const rapidjson::Value &getValue(const rapidjson::Value &obj, const char *key);
static double getDouble(const rapidjson::Value &obj, const char *key, double defaultValue = 0);
static int getInt(const rapidjson::Value &obj, const char *key, int defaultValue = 0);
static int64_t getInt64(const rapidjson::Value &obj, const char *key, int64_t defaultValue = 0);
static String getString(const rapidjson::Value &obj, const char *key, size_t maxSize);
static uint64_t getUint64(const rapidjson::Value &obj, const char *key, uint64_t defaultValue = 0);
static unsigned getUint(const rapidjson::Value &obj, const char *key, unsigned defaultValue = 0);
@ -66,6 +59,7 @@ private:
class JsonReader : public IJsonReader
{
public:
JsonReader();
inline JsonReader(const rapidjson::Value &obj) : m_obj(obj) {}
inline bool getBool(const char *key, bool defaultValue = false) const override { return Json::getBool(m_obj, key, defaultValue); }
@ -73,8 +67,11 @@ public:
inline const rapidjson::Value &getArray(const char *key) const override { return Json::getArray(m_obj, key); }
inline const rapidjson::Value &getObject(const char *key) const override { return Json::getObject(m_obj, key); }
inline const rapidjson::Value &getValue(const char *key) const override { return Json::getValue(m_obj, key); }
inline const rapidjson::Value &object() const override { return m_obj; }
inline double getDouble(const char *key, double defaultValue = 0) const override { return Json::getDouble(m_obj, key, defaultValue); }
inline int getInt(const char *key, int defaultValue = 0) const override { return Json::getInt(m_obj, key, defaultValue); }
inline int64_t getInt64(const char *key, int64_t defaultValue = 0) const override { return Json::getInt64(m_obj, key, defaultValue); }
inline String getString(const char *key, size_t maxSize) const override { return Json::getString(m_obj, key, maxSize); }
inline uint64_t getUint64(const char *key, uint64_t defaultValue = 0) const override { return Json::getUint64(m_obj, key, defaultValue); }
inline unsigned getUint(const char *key, unsigned defaultValue = 0) const override { return Json::getUint(m_obj, key, defaultValue); }

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -181,6 +175,28 @@ const rapidjson::Value &xmrig::JsonChain::getValue(const char *key) const
}
const rapidjson::Value &xmrig::JsonChain::object() const
{
assert(false);
return m_chain.back();
}
double xmrig::JsonChain::getDouble(const char *key, double defaultValue) const
{
for (auto it = m_chain.rbegin(); it != m_chain.rend(); ++it) {
auto i = it->FindMember(key);
if (i != it->MemberEnd() && (i->value.IsDouble() || i->value.IsLosslessDouble())) {
return i->value.GetDouble();
}
}
return defaultValue;
}
int xmrig::JsonChain::getInt(const char *key, int defaultValue) const
{
for (auto it = m_chain.rbegin(); it != m_chain.rend(); ++it) {
@ -207,6 +223,24 @@ int64_t xmrig::JsonChain::getInt64(const char *key, int64_t defaultValue) const
}
xmrig::String xmrig::JsonChain::getString(const char *key, size_t maxSize) const
{
for (auto it = m_chain.rbegin(); it != m_chain.rend(); ++it) {
auto i = it->FindMember(key);
if (i != it->MemberEnd() && i->value.IsString()) {
if (maxSize == 0 || i->value.GetStringLength() <= maxSize) {
return i->value.GetString();
}
return { i->value.GetString(), maxSize };
}
}
return {};
}
uint64_t xmrig::JsonChain::getUint64(const char *key, uint64_t defaultValue) const
{
for (auto it = m_chain.rbegin(); it != m_chain.rend(); ++it) {

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -59,8 +53,11 @@ protected:
const rapidjson::Value &getArray(const char *key) const override;
const rapidjson::Value &getObject(const char *key) const override;
const rapidjson::Value &getValue(const char *key) const override;
const rapidjson::Value &object() const override;
double getDouble(const char *key, double defaultValue = 0) const override;
int getInt(const char *key, int defaultValue = 0) const override;
int64_t getInt64(const char *key, int64_t defaultValue = 0) const override;
String getString(const char *key, size_t maxSize) const override;
uint64_t getUint64(const char *key, uint64_t defaultValue = 0) const override;
unsigned getUint(const char *key, unsigned defaultValue = 0) const override;

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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,16 +24,33 @@
namespace xmrig {
static const char *k2_0 = "2.0";
static const char *kId = "id";
static const char *kJsonRPC = "jsonrpc";
static const char *kMethod = "method";
const char *JsonRequest::kParams = "params";
const char *JsonRequest::k2_0 = "2.0";
const char *JsonRequest::kId = "id";
const char *JsonRequest::kJsonRPC = "jsonrpc";
const char *JsonRequest::kMethod = "method";
const char *JsonRequest::kOK = "OK";
const char *JsonRequest::kParams = "params";
const char *JsonRequest::kResult = "result";
const char *JsonRequest::kStatus = "status";
const char *JsonRequest::kParseError = "parse error";
const char *JsonRequest::kInvalidRequest = "invalid request";
const char *JsonRequest::kMethodNotFound = "method not found";
const char *JsonRequest::kInvalidParams = "invalid params";
const char *JsonRequest::kInternalError = "internal error";
static uint64_t nextId = 0;
} // namespace xmrig
rapidjson::Document xmrig::JsonRequest::create(const char *method)
{
return create(++nextId, method);
}
rapidjson::Document xmrig::JsonRequest::create(int64_t id, const char *method)
{
using namespace rapidjson;
@ -54,7 +65,13 @@ rapidjson::Document xmrig::JsonRequest::create(int64_t id, const char *method)
}
void xmrig::JsonRequest::create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value &params)
uint64_t xmrig::JsonRequest::create(rapidjson::Document &doc, const char *method, rapidjson::Value &params)
{
return create(doc, ++nextId, method, params);
}
uint64_t xmrig::JsonRequest::create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value &params)
{
using namespace rapidjson;
auto &allocator = doc.GetAllocator();
@ -63,4 +80,6 @@ void xmrig::JsonRequest::create(rapidjson::Document &doc, int64_t id, const char
doc.AddMember(StringRef(kJsonRPC), StringRef(k2_0), allocator);
doc.AddMember(StringRef(kMethod), StringRef(method), allocator);
doc.AddMember(StringRef(kParams), params, allocator);
return id;
}

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -35,10 +29,31 @@ namespace xmrig {
class JsonRequest
{
public:
static const char *k2_0;
static const char *kId;
static const char *kJsonRPC;
static const char *kMethod;
static const char *kOK;
static const char *kParams;
static const char *kResult;
static const char *kStatus;
static const char *kParseError;
static const char *kInvalidRequest;
static const char *kMethodNotFound;
static const char *kInvalidParams;
static const char *kInternalError;
constexpr static int kParseErrorCode = -32700;
constexpr static int kInvalidRequestCode = -32600;
constexpr static int kMethodNotFoundCode = -32601;
constexpr static int kInvalidParamsCode = -32602;
constexpr static int kInternalErrorCode = -32603;
static rapidjson::Document create(const char *method);
static rapidjson::Document create(int64_t id, const char *method);
static void create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value &params);
static uint64_t create(rapidjson::Document &doc, const char *method, rapidjson::Value &params);
static uint64_t create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value &params);
};

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -43,7 +37,7 @@ bool xmrig::Json::get(const char *fileName, rapidjson::Document &doc)
rapidjson::IStreamWrapper isw(ifs);
doc.ParseStream<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(isw);
return !doc.HasParseError() && doc.IsObject();
return !doc.HasParseError() && (doc.IsObject() || doc.IsArray());
}
@ -56,7 +50,10 @@ bool xmrig::Json::save(const char *fileName, const rapidjson::Document &doc)
rapidjson::OStreamWrapper osw(ofs);
rapidjson::PrettyWriter<rapidjson::OStreamWrapper> writer(osw);
# ifdef XMRIG_JSON_SINGLE_LINE_ARRAY
writer.SetFormatOptions(rapidjson::kFormatSingleLineArray);
# endif
doc.Accept(writer);

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -96,7 +90,7 @@ bool xmrig::Json::get(const char *fileName, rapidjson::Document &doc)
IStreamWrapper isw(ifs);
doc.ParseStream<kParseCommentsFlag | kParseTrailingCommasFlag>(isw);
return !doc.HasParseError() && doc.IsObject();
return !doc.HasParseError() && (doc.IsObject() || doc.IsArray());
}
@ -127,7 +121,10 @@ bool xmrig::Json::save(const char *fileName, const rapidjson::Document &doc)
OStreamWrapper osw(ofs);
PrettyWriter<OStreamWrapper> writer(osw);
# ifdef XMRIG_JSON_SINGLE_LINE_ARRAY
writer.SetFormatOptions(kFormatSingleLineArray);
# endif
doc.Accept(writer);

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2019 Spudz76 <https://github.com/Spudz76>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2019 Spudz76 <https://github.com/Spudz76>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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,6 +25,7 @@
#include <algorithm>
#include <cassert>
#include <cstring>
#include <ctime>
#include <mutex>
@ -76,7 +71,7 @@ public:
inline ~LogPrivate()
{
for (ILogBackend *backend : m_backends) {
for (auto backend : m_backends) {
delete backend;
}
}
@ -96,7 +91,7 @@ public:
return;
}
timestamp(level, size, offset);
const uint64_t ts = timestamp(level, size, offset);
color(level, size);
const int rc = vsnprintf(m_buf + size, sizeof (m_buf) - offset - 32, fmt, args);
@ -114,9 +109,9 @@ public:
}
if (!m_backends.empty()) {
for (ILogBackend *backend : m_backends) {
backend->print(level, m_buf, offset, size, true);
backend->print(level, txt.c_str(), offset ? (offset - 11) : 0, txt.size(), false);
for (auto backend : m_backends) {
backend->print(ts, level, m_buf, offset, size, true);
backend->print(ts, level, txt.c_str(), offset ? (offset - 11) : 0, txt.size(), false);
}
}
else {
@ -127,14 +122,15 @@ public:
private:
inline void timestamp(Log::Level level, size_t &size, size_t &offset)
inline uint64_t timestamp(Log::Level level, size_t &size, size_t &offset)
{
const uint64_t ms = Chrono::currentMSecsSinceEpoch();
if (level == Log::NONE) {
return;
return ms;
}
const uint64_t ms = Chrono::currentMSecsSinceEpoch();
time_t now = ms / 1000;
time_t now = ms / 1000;
tm stime{};
# ifdef _WIN32
@ -156,6 +152,8 @@ private:
if (rc > 0) {
size = offset = static_cast<size_t>(rc);
}
return ms;
}
@ -195,10 +193,10 @@ private:
};
bool Log::m_background = false;
bool Log::m_colors = true;
LogPrivate *Log::d = new LogPrivate();
uint32_t Log::m_verbose = 0;
bool Log::m_background = false;
bool Log::m_colors = true;
LogPrivate *Log::d = nullptr;
uint32_t Log::m_verbose = 0;
} /* namespace xmrig */
@ -207,6 +205,8 @@ uint32_t Log::m_verbose = 0;
void xmrig::Log::add(ILogBackend *backend)
{
assert(d != nullptr);
if (d) {
d->add(backend);
}
@ -220,6 +220,12 @@ void xmrig::Log::destroy()
}
void xmrig::Log::init()
{
d = new LogPrivate();
}
void xmrig::Log::print(const char *fmt, ...)
{
if (!d) {

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2019 Spudz76 <https://github.com/Spudz76>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2019 Spudz76 <https://github.com/Spudz76>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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,6 +51,7 @@ public:
static void add(ILogBackend *backend);
static void destroy();
static void init();
static void print(const char *fmt, ...);
static void print(Level level, const char *fmt, ...);
@ -71,9 +66,8 @@ public:
private:
static bool m_background;
static bool m_colors;
static uint32_t m_verbose;
static LogPrivate *d;
static uint32_t m_verbose;
};

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2019 Spudz76 <https://github.com/Spudz76>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2019 Spudz76 <https://github.com/Spudz76>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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,14 +18,13 @@
*/
#include <cstdio>
#include "base/io/log/backends/ConsoleLog.h"
#include "base/io/log/Log.h"
#include "base/kernel/config/Title.h"
#include "base/tools/Handle.h"
#include "version.h"
#include <cstdio>
xmrig::ConsoleLog::ConsoleLog(const Title &title)
@ -75,7 +68,7 @@ xmrig::ConsoleLog::~ConsoleLog()
}
void xmrig::ConsoleLog::print(int, const char *line, size_t, size_t size, bool colors)
void xmrig::ConsoleLog::print(uint64_t, int, const char *line, size_t, size_t size, bool colors)
{
if (!m_tty || Log::isColors() != colors) {
return;

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2019 Spudz76 <https://github.com/Spudz76>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2019 Spudz76 <https://github.com/Spudz76>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -50,7 +44,7 @@ public:
~ConsoleLog() override;
protected:
void print(int level, const char *line, size_t offset, size_t size, bool colors) override;
void print(uint64_t timestamp, int level, const char *line, size_t offset, size_t size, bool colors) override;
private:
bool isSupported() const;

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2019 Spudz76 <https://github.com/Spudz76>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2019 Spudz76 <https://github.com/Spudz76>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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,7 +31,7 @@ xmrig::FileLog::FileLog(const char *fileName) :
}
void xmrig::FileLog::print(int, const char *line, size_t, size_t size, bool colors)
void xmrig::FileLog::print(uint64_t, int, const char *line, size_t, size_t size, bool colors)
{
if (!m_writer.isOpen() || colors) {
return;

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2019 Spudz76 <https://github.com/Spudz76>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2019 Spudz76 <https://github.com/Spudz76>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -40,7 +34,7 @@ public:
FileLog(const char *fileName);
protected:
void print(int level, const char *line, size_t offset, size_t size, bool colors) override;
void print(uint64_t timestamp, int level, const char *line, size_t offset, size_t size, bool colors) override;
private:
FileLogWriter m_writer;

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2019 Spudz76 <https://github.com/Spudz76>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2019 Spudz76 <https://github.com/Spudz76>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -43,7 +37,7 @@ xmrig::SysLog::~SysLog()
}
void xmrig::SysLog::print(int level, const char *line, size_t offset, size_t, bool colors)
void xmrig::SysLog::print(uint64_t, int level, const char *line, size_t offset, size_t, bool colors)
{
if (colors) {
return;

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2019 Spudz76 <https://github.com/Spudz76>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2019 Spudz76 <https://github.com/Spudz76>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 +30,13 @@ namespace xmrig {
class SysLog : public ILogBackend
{
public:
XMRIG_DISABLE_COPY_MOVE(SysLog)
SysLog();
~SysLog() override;
protected:
void print(int level, const char *line, size_t offset, size_t size, bool colors) override;
void print(uint64_t timestamp, int level, const char *line, size_t offset, size_t size, bool colors) override;
};

View file

@ -166,6 +166,7 @@ private:
xmrig::Base::Base(Process *process)
: d_ptr(new BasePrivate(process))
{
Log::init();
}

View file

@ -26,6 +26,7 @@
#include <thread>
#ifndef XMRIG_OS_APPLE
bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id)
{
auto cpu = static_cast<HwlocCpuInfo *>(Cpu::info());
@ -44,3 +45,4 @@ bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
return result;
}
#endif

View file

@ -19,8 +19,6 @@
#include <IOKit/IOKitLib.h>
#include <IOKit/ps/IOPowerSources.h>
#include <mach/thread_act.h>
#include <mach/thread_policy.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
@ -38,7 +36,14 @@ char *xmrig::Platform::createUserAgent()
constexpr const size_t max = 256;
char *buf = new char[max]();
int length = snprintf(buf, max, "%s/%s (Macintosh; Intel Mac OS X) libuv/%s", APP_NAME, APP_VERSION, uv_version_string());
int length = snprintf(buf, max,
"%s/%s (Macintosh; macOS"
# ifdef XMRIG_ARM
"; arm64"
# else
"; x86_64"
# endif
") libuv/%s", APP_NAME, APP_VERSION, uv_version_string());
# ifdef __clang__
length += snprintf(buf + length, max - length, " clang/%d.%d.%d", __clang_major__, __clang_minor__, __clang_patchlevel__);
@ -50,18 +55,10 @@ char *xmrig::Platform::createUserAgent()
}
#ifndef XMRIG_FEATURE_HWLOC
bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id)
{
thread_port_t mach_thread;
thread_affinity_policy_data_t policy = { static_cast<integer_t>(cpu_id) };
mach_thread = pthread_mach_thread_np(pthread_self());
const bool result = (thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1) == KERN_SUCCESS);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
return result;
return true;
}
#endif
void xmrig::Platform::setProcessPriority(int)

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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

View file

@ -0,0 +1,31 @@
/* XMRig
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_CONSTANTS_H
#define XMRIG_CONSTANTS_H
#include <cstddef>
#include <cstdint>
constexpr size_t XMRIG_NET_BUFFER_CHUNK_SIZE = 64 * 1024;
constexpr size_t XMRIG_NET_BUFFER_INIT_CHUNKS = 4;
#endif /* XMRIG_CONSTANTS_H */

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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,6 +21,8 @@
#include "3rdparty/rapidjson/fwd.h"
#include "base/tools/Object.h"
#include "base/tools/String.h"
namespace xmrig {
@ -35,7 +31,10 @@ namespace xmrig {
class IJsonReader
{
public:
virtual ~IJsonReader() = default;
XMRIG_DISABLE_COPY_MOVE(IJsonReader)
IJsonReader() = default;
virtual ~IJsonReader() = default;
virtual bool getBool(const char *key, bool defaultValue = false) const = 0;
virtual bool isEmpty() const = 0;
@ -43,8 +42,11 @@ public:
virtual const rapidjson::Value &getArray(const char *key) const = 0;
virtual const rapidjson::Value &getObject(const char *key) const = 0;
virtual const rapidjson::Value &getValue(const char *key) const = 0;
virtual const rapidjson::Value &object() const = 0;
virtual double getDouble(const char *key, double defaultValue = 0) const = 0;
virtual int getInt(const char *key, int defaultValue = 0) const = 0;
virtual int64_t getInt64(const char *key, int64_t defaultValue = 0) const = 0;
virtual String getString(const char *key, size_t maxSize) const = 0;
virtual uint64_t getUint64(const char *key, uint64_t defaultValue = 0) const = 0;
virtual unsigned getUint(const char *key, unsigned defaultValue = 0) const = 0;
};

View file

@ -1,13 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2019 Spudz76 <https://github.com/Spudz76>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 +20,11 @@
#define XMRIG_ILOGBACKEND_H
#include <stdarg.h>
#include <stddef.h>
#include "base/tools/Object.h"
#include <cstdarg>
#include <cstddef>
namespace xmrig {
@ -37,9 +33,12 @@ namespace xmrig {
class ILogBackend
{
public:
virtual ~ILogBackend() = default;
XMRIG_DISABLE_COPY_MOVE(ILogBackend)
virtual void print(int level, const char *line, size_t offset, size_t size, bool colors) = 0;
ILogBackend() = default;
virtual ~ILogBackend() = default;
virtual void print(uint64_t timestamp, int level, const char *line, size_t offset, size_t size, bool colors) = 0;
};

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -95,7 +95,7 @@ void xmrig::FetchRequest::setBody(const rapidjson::Value &value)
}
void xmrig::fetch(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener, int type)
void xmrig::fetch(const char *tag, FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener, int type, uint64_t rpcId)
{
# ifdef APP_DEBUG
LOG_DEBUG(CYAN("http%s://%s:%u ") MAGENTA_BOLD("\"%s %s\"") BLACK_BOLD(" body: ") CYAN_BOLD("%zu") BLACK_BOLD(" bytes"),
@ -109,14 +109,15 @@ void xmrig::fetch(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listen
HttpClient *client;
# ifdef XMRIG_FEATURE_TLS
if (req.tls) {
client = new HttpsClient(std::move(req), listener);
client = new HttpsClient(tag, std::move(req), listener);
}
else
# endif
{
client = new HttpClient(std::move(req), listener);
client = new HttpClient(tag, std::move(req), listener);
}
client->userType = type;
client->rpcId = rpcId;
client->connect();
}

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -58,10 +58,11 @@ public:
String host;
String path;
uint16_t port = 0;
uint64_t timeout = 0;
};
void fetch(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener, int type = 0);
void fetch(const char *tag, FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener, int type = 0, uint64_t rpcId = 0);
} // namespace xmrig

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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,6 +24,7 @@
#include "base/kernel/Platform.h"
#include "base/net/dns/Dns.h"
#include "base/net/tools/NetBuffer.h"
#include "base/tools/Timer.h"
#include <sstream>
@ -44,21 +39,20 @@ static const char *kCRLF = "\r\n";
} // namespace xmrig
xmrig::HttpClient::HttpClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener) :
xmrig::HttpClient::HttpClient(const char *tag, FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener) :
HttpContext(HTTP_RESPONSE, listener),
m_tag(tag),
m_req(std::move(req))
{
method = m_req.method;
url = std::move(m_req.path);
body = std::move(m_req.body);
headers = std::move(m_req.headers);
m_dns = new Dns(this);
}
m_dns = std::make_shared<Dns>(this);
xmrig::HttpClient::~HttpClient()
{
delete m_dns;
if (m_req.timeout) {
m_timer = std::make_shared<Timer>(this, m_req.timeout, 0);
}
}
@ -74,7 +68,7 @@ void xmrig::HttpClient::onResolved(const Dns &dns, int status)
if (status < 0 && dns.isEmpty()) {
if (!isQuiet()) {
LOG_ERR("[%s:%d] DNS error: \"%s\"", dns.host().data(), port(), uv_strerror(status));
LOG_ERR("%s " RED("DNS error: ") RED_BOLD("\"%s\""), tag(), uv_strerror(status));
}
return;
@ -91,6 +85,12 @@ void xmrig::HttpClient::onResolved(const Dns &dns, int status)
}
void xmrig::HttpClient::onTimer(const Timer *)
{
close(UV_ETIMEDOUT);
}
void xmrig::HttpClient::handshake()
{
headers.insert({ "Host", host() });
@ -135,8 +135,12 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status)
}
if (status < 0) {
if (status == UV_ECANCELED) {
status = UV_ETIMEDOUT;
}
if (!client->isQuiet()) {
LOG_ERR("[%s:%d] connect error: \"%s\"", client->m_dns->host().data(), client->port(), uv_strerror(status));
LOG_ERR("%s " RED("connect error: ") RED_BOLD("\"%s\""), client->tag(), uv_strerror(status));
}
return client->close(status);
@ -151,7 +155,7 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status)
client->read(buf->base, static_cast<size_t>(nread));
} else {
if (!client->isQuiet() && nread != UV_EOF) {
LOG_ERR("[%s:%d] read error: \"%s\"", client->m_dns->host().data(), client->port(), uv_strerror(static_cast<int>(nread)));
LOG_ERR("%s " RED("read error: ") RED_BOLD("\"%s\""), client->tag(), uv_strerror(static_cast<int>(nread)));
}
client->close(static_cast<int>(nread));

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -29,6 +23,7 @@
#include "base/kernel/interfaces/IDnsListener.h"
#include "base/kernel/interfaces/ITimerListener.h"
#include "base/net/http/Fetch.h"
#include "base/net/http/HttpContext.h"
#include "base/tools/Object.h"
@ -40,22 +35,24 @@ namespace xmrig {
class String;
class HttpClient : public HttpContext, public IDnsListener
class HttpClient : public HttpContext, public IDnsListener, public ITimerListener
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpClient);
HttpClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener);
~HttpClient() override;
HttpClient(const char *tag, FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener);
~HttpClient() override = default;
inline bool isQuiet() const { return m_req.quiet; }
inline const char *host() const override { return m_req.host; }
inline const char *tag() const { return m_tag; }
inline uint16_t port() const override { return m_req.port; }
bool connect();
protected:
void onResolved(const Dns &dns, int status) override;
void onTimer(const Timer *timer) override;
virtual void handshake();
virtual void read(const char *data, size_t size);
@ -66,8 +63,10 @@ protected:
private:
static void onConnect(uv_connect_t *req, int status);
Dns *m_dns;
const char *m_tag;
FetchRequest m_req;
std::shared_ptr<Dns> m_dns;
std::shared_ptr<Timer> m_timer;
};

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -160,6 +154,10 @@ uint64_t xmrig::HttpContext::elapsed() const
void xmrig::HttpContext::close(int status)
{
if (!get(id())) {
return;
}
auto listener = httpListener();
if (status < 0 && listener) {
@ -262,7 +260,7 @@ void xmrig::HttpContext::attach(http_parser_settings *settings)
settings->on_body = [](http_parser *parser, const char *at, size_t len) -> int
{
static_cast<HttpContext*>(parser->data)->body += std::string(at, len);
static_cast<HttpContext*>(parser->data)->body.append(at, len);
return 0;
};

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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,7 +25,7 @@
#include "base/net/https/HttpsClient.h"
#include "base/io/log/Log.h"
#include "base/tools/Buffer.h"
#include "base/tools/Cvt.h"
#ifdef _MSC_VER
@ -39,8 +33,8 @@
#endif
xmrig::HttpsClient::HttpsClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener) :
HttpClient(std::move(req), listener)
xmrig::HttpsClient::HttpsClient(const char *tag, FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener) :
HttpClient(tag, std::move(req), listener)
{
m_ctx = SSL_CTX_new(SSLv23_method());
assert(m_ctx != nullptr);
@ -182,7 +176,7 @@ bool xmrig::HttpsClient::verifyFingerprint(X509 *cert)
return false;
}
Buffer::toHex(md, 32, m_fingerprint);
Cvt::toHex(m_fingerprint, sizeof(m_fingerprint), md, 32);
return req().fingerprint.isNull() || strncasecmp(m_fingerprint, req().fingerprint.data(), 64) == 0;
}

View file

@ -1,13 +1,7 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 +40,7 @@ class HttpsClient : public HttpClient
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpsClient)
HttpsClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener);
HttpsClient(const char *tag, FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener);
~HttpsClient() override;
const char *tlsFingerprint() const override;

View file

@ -50,7 +50,7 @@
#include "base/net/dns/Dns.h"
#include "base/net/stratum/Socks5.h"
#include "base/net/tools/NetBuffer.h"
#include "base/tools/Buffer.h"
#include "base/tools/Cvt.h"
#include "base/tools/Chrono.h"
#include "net/JobResult.h"
@ -204,11 +204,8 @@ int64_t xmrig::Client::submit(const JobResult &result)
char *nonce = m_sendBuf.data();
char *data = m_sendBuf.data() + 16;
Buffer::toHex(reinterpret_cast<const char*>(&result.nonce), 4, nonce);
nonce[8] = '\0';
Buffer::toHex(result.result(), 32, data);
data[64] = '\0';
Cvt::toHex(nonce, sizeof(uint32_t) * 2 + 1, reinterpret_cast<const uint8_t *>(&result.nonce), sizeof(uint32_t));
Cvt::toHex(data, 65, result.result(), 32);
# endif
Document doc(kObjectType);

View file

@ -36,7 +36,7 @@
#include "base/net/http/HttpData.h"
#include "base/net/http/HttpListener.h"
#include "base/net/stratum/SubmitResult.h"
#include "base/tools/Buffer.h"
#include "base/tools/Cvt.h"
#include "base/tools/Timer.h"
#include "net/JobResult.h"
@ -54,7 +54,7 @@ static const char *kHash = "hash";
static const char *kHeight = "height";
static const char *kJsonRPC = "/json_rpc";
static const size_t BlobReserveSize = 8;
static constexpr size_t kBlobReserveSize = 8;
}
@ -104,7 +104,7 @@ int64_t xmrig::DaemonClient::submit(const JobResult &result)
# ifdef XMRIG_PROXY_PROJECT
memcpy(data + 78, result.nonce, 8);
# else
Buffer::toHex(reinterpret_cast<const uint8_t *>(&result.nonce), 4, data + 78);
Cvt::toHex(data + 78, 9, reinterpret_cast<const uint8_t *>(&result.nonce), 4);
# endif
using namespace rapidjson;
@ -227,7 +227,7 @@ bool xmrig::DaemonClient::parseJob(const rapidjson::Value &params, int *code)
m_blockhashingblob = Json::getString(params, "blockhashing_blob");
if (m_apiVersion == API_DERO) {
const uint64_t offset = Json::getUint64(params, "reserved_offset");
Buffer::toHex(Buffer::randomBytes(BlobReserveSize).data(), BlobReserveSize, m_blockhashingblob.data() + offset * 2);
Cvt::toHex(m_blockhashingblob.data() + offset * 2, kBlobReserveSize * 2 + 1, Cvt::randomBytes(kBlobReserveSize).data(), kBlobReserveSize);
}
if (blocktemplate.isNull() || !job.setBlob(m_blockhashingblob)) {
@ -315,10 +315,10 @@ int64_t xmrig::DaemonClient::getBlockTemplate()
Value params(kObjectType);
params.AddMember("wallet_address", m_user.toJSON(), allocator);
if (m_apiVersion == API_DERO) {
params.AddMember("reserve_size", static_cast<uint64_t>(BlobReserveSize), allocator);
params.AddMember("reserve_size", static_cast<uint64_t>(kBlobReserveSize), allocator);
}
else {
params.AddMember("extra_nonce", Buffer::randomBytes(BlobReserveSize).toHex().toJSON(doc), allocator);
params.AddMember("extra_nonce", Cvt::toHex(Cvt::randomBytes(kBlobReserveSize)).toJSON(doc), allocator);
}
JsonRequest::create(doc, m_sequence, "getblocktemplate", params);
@ -330,7 +330,7 @@ int64_t xmrig::DaemonClient::getBlockTemplate()
int64_t xmrig::DaemonClient::rpcSend(const rapidjson::Document &doc)
{
FetchRequest req(HTTP_POST, m_pool.host(), m_pool.port(), kJsonRPC, doc, m_pool.isTLS(), isQuiet());
fetch(std::move(req), m_httpListener);
fetch(tag(), std::move(req), m_httpListener);
return m_sequence++;
}
@ -357,7 +357,7 @@ void xmrig::DaemonClient::retry()
void xmrig::DaemonClient::send(const char *path)
{
FetchRequest req(HTTP_GET, m_pool.host(), m_pool.port(), path, m_pool.isTLS(), isQuiet());
fetch(std::move(req), m_httpListener);
fetch(tag(), std::move(req), m_httpListener);
}

View file

@ -31,6 +31,7 @@
#include "base/net/stratum/Job.h"
#include "base/tools/Buffer.h"
#include "base/tools/Cvt.h"
xmrig::Job::Job(bool nicehash, const Algorithm &algorithm, const String &clientId) :
@ -59,11 +60,13 @@ bool xmrig::Job::setBlob(const char *blob)
}
m_size /= 2;
if (m_size < 76 || m_size >= sizeof(m_blob)) {
const size_t minSize = nonceOffset() + nonceSize();
if (m_size < minSize || m_size >= sizeof(m_blob)) {
return false;
}
if (!Buffer::fromHex(blob, m_size * 2, m_blob)) {
if (!Cvt::fromHex(m_blob, sizeof(m_blob), blob, m_size * 2)) {
return false;
}
@ -90,9 +93,9 @@ bool xmrig::Job::setSeedHash(const char *hash)
m_rawSeedHash = hash;
# endif
m_seed = Buffer::fromHex(hash, kMaxSeedSize * 2);
m_seed = Cvt::fromHex(hash, kMaxSeedSize * 2);
return !m_seed.isEmpty();
return !m_seed.empty();
}
@ -102,27 +105,14 @@ bool xmrig::Job::setTarget(const char *target)
return false;
}
const size_t len = strlen(target);
const auto raw = Cvt::fromHex(target, strlen(target));
const size_t size = raw.size();
if (len <= 8) {
uint32_t tmp = 0;
char str[8];
memcpy(str, target, len);
if (!Buffer::fromHex(str, 8, reinterpret_cast<uint8_t *>(&tmp)) || tmp == 0) {
return false;
}
m_target = 0xFFFFFFFFFFFFFFFFULL / (0xFFFFFFFFULL / static_cast<uint64_t>(tmp));
if (size == 4) {
m_target = 0xFFFFFFFFFFFFFFFFULL / (0xFFFFFFFFULL / uint64_t(*reinterpret_cast<const uint32_t *>(raw.data())));
}
else if (len <= 16) {
m_target = 0;
char str[16];
memcpy(str, target, len);
if (!Buffer::fromHex(str, 16, reinterpret_cast<uint8_t *>(&m_target)) || m_target == 0) {
return false;
}
else if (size == 8) {
m_target = *reinterpret_cast<const uint64_t *>(raw.data());
}
else {
return false;

View file

@ -154,7 +154,7 @@ void xmrig::SelfSelectClient::getBlockTemplate()
JsonRequest::create(doc, m_sequence++, "getblocktemplate", params);
FetchRequest req(HTTP_POST, pool().daemon().host(), pool().daemon().port(), "/json_rpc", doc, pool().daemon().isTLS(), isQuiet());
fetch(std::move(req), m_httpListener);
fetch(tag(), std::move(req), m_httpListener);
}

View file

@ -27,7 +27,7 @@
#include "base/net/stratum/Tls.h"
#include "base/io/log/Log.h"
#include "base/net/stratum/Client.h"
#include "base/tools/Buffer.h"
#include "base/tools/Cvt.h"
#ifdef _MSC_VER
@ -183,7 +183,7 @@ bool xmrig::Client::Tls::verifyFingerprint(X509 *cert)
return false;
}
Buffer::toHex(md, 32, m_fingerprint);
Cvt::toHex(m_fingerprint, sizeof(m_fingerprint), md, 32);
const char *fingerprint = m_client->m_pool.fingerprint();
return fingerprint == nullptr || strncasecmp(m_fingerprint, fingerprint, 64) == 0;

View file

@ -31,6 +31,7 @@
#include "base/net/http/HttpData.h"
#include "base/net/http/HttpListener.h"
#include "base/net/stratum/benchmark/BenchConfig.h"
#include "base/tools/Cvt.h"
#include "version.h"
@ -217,7 +218,7 @@ bool xmrig::BenchClient::setSeed(const char *seed)
return false;
}
if (!Buffer::fromHex(seed, size * 2, m_job.blob())) {
if (!Cvt::fromHex(m_job.blob(), m_job.size(), seed, size * 2)) {
return false;
}
@ -321,7 +322,7 @@ void xmrig::BenchClient::send(Request request)
case GET_BENCH:
{
FetchRequest req(HTTP_GET, m_ip, BenchConfig::kApiPort, fmt::format("/1/benchmark/{}", m_job.id()).c_str(), BenchConfig::kApiTLS, true);
fetch(std::move(req), m_httpListener);
fetch(tag(), std::move(req), m_httpListener);
}
break;
@ -335,7 +336,7 @@ void xmrig::BenchClient::send(Request request)
doc.AddMember("cpu", Cpu::toJSON(doc), allocator);
FetchRequest req(HTTP_POST, m_ip, BenchConfig::kApiPort, "/1/benchmark", doc, BenchConfig::kApiTLS, true);
fetch(std::move(req), m_httpListener);
fetch(tag(), std::move(req), m_httpListener);
}
break;
@ -374,6 +375,6 @@ void xmrig::BenchClient::update(const rapidjson::Value &body)
FetchRequest req(HTTP_PATCH, m_ip, BenchConfig::kApiPort, fmt::format("/1/benchmark/{}", m_job.id()).c_str(), body, BenchConfig::kApiTLS, true);
req.headers.insert({ "Authorization", fmt::format("Bearer {}", m_token)});
fetch(std::move(req), m_httpListener);
fetch(tag(), std::move(req), m_httpListener);
}
#endif

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -20,6 +20,7 @@
#include "base/net/tls/ServerTls.h"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <openssl/ssl.h>
@ -39,11 +40,23 @@ xmrig::ServerTls::~ServerTls()
}
bool xmrig::ServerTls::isHTTP(const char *data, size_t size)
{
assert(size > 0);
static const char test[6] = "GET /";
return size > 0 && memcmp(data, test, std::min(size, sizeof(test) - 1)) == 0;
}
bool xmrig::ServerTls::isTLS(const char *data, size_t size)
{
assert(size > 0);
static const uint8_t test[3] = { 0x16, 0x03, 0x01 };
return size >= sizeof(test) && memcmp(data, test, sizeof(test)) == 0;
return size > 0 && memcmp(data, test, std::min(size, sizeof(test))) == 0;
}

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -41,6 +41,7 @@ public:
ServerTls(SSL_CTX *ctx);
virtual ~ServerTls();
static bool isHTTP(const char *data, size_t size);
static bool isTLS(const char *data, size_t size);
bool send(const char *data, size_t size);

View file

@ -19,8 +19,9 @@
#include "base/net/tools/LineReader.h"
#include "base/net/tools/NetBuffer.h"
#include "base/kernel/constants.h"
#include "base/kernel/interfaces/ILineListener.h"
#include "base/net/tools/NetBuffer.h"
#include <cassert>
#include <cstring>
@ -55,8 +56,8 @@ void xmrig::LineReader::reset()
void xmrig::LineReader::add(const char *data, size_t size)
{
if (size > NetBuffer::kChunkSize - m_pos) {
// it breakes correctness silently for long lines
if (size + m_pos > XMRIG_NET_BUFFER_CHUNK_SIZE) {
// it breaks correctness silently for long lines
return;
}

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -20,10 +20,11 @@
#define XMRIG_MEMPOOL_H
#include <map>
#include <set>
#include <array>
#include <cassert>
#include <cstddef>
#include <map>
#include <set>
namespace xmrig {

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -17,8 +17,9 @@
*/
#include "base/net/tools/MemPool.h"
#include "base/net/tools/NetBuffer.h"
#include "base/kernel/constants.h"
#include "base/net/tools/MemPool.h"
#include <cassert>
@ -28,14 +29,13 @@
namespace xmrig {
static constexpr size_t kInitSize = 4;
static MemPool<NetBuffer::kChunkSize, kInitSize> *pool = nullptr;
static MemPool<XMRIG_NET_BUFFER_CHUNK_SIZE, XMRIG_NET_BUFFER_INIT_CHUNKS> *pool = nullptr;
inline MemPool<NetBuffer::kChunkSize, kInitSize> *getPool()
inline MemPool<XMRIG_NET_BUFFER_CHUNK_SIZE, XMRIG_NET_BUFFER_INIT_CHUNKS> *getPool()
{
if (!pool) {
pool = new MemPool<NetBuffer::kChunkSize, kInitSize>();
pool = new MemPool<XMRIG_NET_BUFFER_CHUNK_SIZE, XMRIG_NET_BUFFER_INIT_CHUNKS>();
}
return pool;
@ -67,17 +67,25 @@ void xmrig::NetBuffer::destroy()
void xmrig::NetBuffer::onAlloc(uv_handle_t *, size_t, uv_buf_t *buf)
{
buf->base = getPool()->allocate();
buf->len = kChunkSize;
buf->len = XMRIG_NET_BUFFER_CHUNK_SIZE;
}
void xmrig::NetBuffer::release(const char *buf)
{
if (buf == nullptr) {
return;
}
getPool()->deallocate(buf);
}
void xmrig::NetBuffer::release(const uv_buf_t *buf)
{
if (buf->base == nullptr) {
return;
}
getPool()->deallocate(buf->base);
}

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -33,8 +33,6 @@ namespace xmrig {
class NetBuffer
{
public:
static constexpr size_t kChunkSize = 16 * 1024;
static char *allocate();
static void destroy();
static void onAlloc(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);

View file

@ -1,231 +0,0 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "base/tools/Buffer.h"
#include <random>
namespace xmrig {
static std::random_device randomDevice;
static std::mt19937 randomEngine(randomDevice());
} // namespace xmrig
static inline uint8_t hf_hex2bin(uint8_t c, bool &err)
{
if (c >= '0' && c <= '9') {
return c - '0';
}
if (c >= 'a' && c <= 'f') {
return c - 'a' + 0xA;
}
if (c >= 'A' && c <= 'F') {
return c - 'A' + 0xA;
}
err = true;
return 0;
}
static inline uint8_t hf_bin2hex(uint8_t c)
{
if (c <= 0x9) {
return '0' + c;
}
return 'a' - 0xA + c;
}
xmrig::Buffer::Buffer(Buffer &&other) noexcept :
m_data(other.m_data),
m_size(other.m_size)
{
other.m_data = nullptr;
other.m_size = 0;
}
xmrig::Buffer::Buffer(const Buffer &other)
{
copy(other.data(), other.size());
}
xmrig::Buffer::Buffer(const char *data, size_t size)
{
copy(data, size);
}
xmrig::Buffer::Buffer(size_t size) :
m_size(size)
{
if (size > 0) {
m_data = new char[size]();
}
}
xmrig::Buffer::~Buffer()
{
delete [] m_data;
}
void xmrig::Buffer::from(const char *data, size_t size)
{
if (m_size > 0) {
if (m_size == size) {
memcpy(m_data, data, m_size);
return;
}
delete [] m_data;
}
copy(data, size);
}
xmrig::Buffer xmrig::Buffer::allocUnsafe(size_t size)
{
if (size == 0) {
return {};
}
Buffer buf;
buf.m_size = size;
buf.m_data = new char[size];
return buf;
}
xmrig::Buffer xmrig::Buffer::randomBytes(const size_t size)
{
Buffer buf(size);
std::uniform_int_distribution<> dis(0, 255);
for (size_t i = 0; i < size; ++i) {
buf.m_data[i] = static_cast<char>(dis(randomEngine));
}
return buf;
}
bool xmrig::Buffer::fromHex(const uint8_t *in, size_t size, uint8_t *out)
{
bool error = false;
for (size_t i = 0; i < size; i += 2) {
out[i / 2] = static_cast<uint8_t>((hf_hex2bin(in[i], error) << 4) | hf_hex2bin(in[i + 1], error));
if (error) {
return false;
}
}
return true;
}
xmrig::Buffer xmrig::Buffer::fromHex(const char *data, size_t size)
{
if (data == nullptr || size % 2 != 0) {
return {};
}
Buffer buf(size / 2);
if (!fromHex(data, size, buf.data())) {
return {};
}
return buf;
}
void xmrig::Buffer::toHex(const uint8_t *in, size_t size, uint8_t *out)
{
for (size_t i = 0; i < size; i++) {
out[i * 2] = hf_bin2hex((in[i] & 0xF0) >> 4);
out[i * 2 + 1] = hf_bin2hex(in[i] & 0x0F);
}
}
xmrig::String xmrig::Buffer::toHex() const
{
if (m_size == 0) {
return String();
}
char *buf = new char[m_size * 2 + 1];
buf[m_size * 2] = '\0';
toHex(m_data, m_size, buf);
return String(buf);
}
void xmrig::Buffer::copy(const char *data, size_t size)
{
if (size == 0) {
m_data = nullptr;
m_size = 0;
return;
}
m_data = new char[size];
m_size = size;
memcpy(m_data, data, m_size);
}
void xmrig::Buffer::move(Buffer &&other)
{
if (m_size > 0) {
delete [] m_data;
}
m_data = other.m_data;
m_size = other.m_size;
other.m_data = nullptr;
other.m_size = 0;
}

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -26,67 +20,14 @@
#define XMRIG_BUFFER_H
#include "base/tools/String.h"
#include <cstdint>
#include <vector>
namespace xmrig {
class Buffer
{
public:
Buffer() = default;
Buffer(Buffer &&other) noexcept;
Buffer(const Buffer &other);
Buffer(const char *data, size_t size);
Buffer(size_t size);
~Buffer();
inline bool isEmpty() const { return size() == 0; }
inline bool isEqual(const Buffer &other) const { return m_size == other.m_size && (m_size == 0 || memcmp(m_data, other.m_data, m_size) == 0); }
inline char *data() { return m_data; }
inline const char *data() const { return m_data; }
inline size_t size() const { return m_size; }
inline void from(const Buffer &other) { from(other.data(), other.size()); }
void from(const char *data, size_t size);
inline bool operator!=(const Buffer &other) const { return !isEqual(other); }
inline bool operator==(const Buffer &other) const { return isEqual(other); }
inline Buffer &operator=(Buffer &&other) noexcept { move(std::move(other)); return *this; }
inline Buffer &operator=(const Buffer &other) { from(other); return *this; }
static Buffer allocUnsafe(size_t size);
static Buffer randomBytes(const size_t size);
static inline Buffer alloc(size_t size) { return Buffer(size); }
inline static bool fromHex(const char *in, size_t size, char *out) { return fromHex(reinterpret_cast<const uint8_t *>(in), size, reinterpret_cast<uint8_t *>(out)); }
inline static bool fromHex(const char *in, size_t size, uint8_t *out) { return fromHex(reinterpret_cast<const uint8_t *>(in), size, out); }
inline static Buffer fromHex(const char *data) { return fromHex(data, strlen(data)); }
inline static Buffer fromHex(const String &str) { return fromHex(str.data(), str.size()); }
inline static String toHex(const char *in, size_t size) { return Buffer(in, size).toHex(); }
inline static String toHex(const uint8_t *in, size_t size) { return Buffer(reinterpret_cast<const char *>(in), size).toHex(); }
inline static void toHex(const char *in, size_t size, char *out) { return toHex(reinterpret_cast<const uint8_t *>(in), size, reinterpret_cast<uint8_t *>(out)); }
inline static void toHex(const uint8_t *in, size_t size, char *out) { return toHex(in, size, reinterpret_cast<uint8_t *>(out)); }
static bool fromHex(const uint8_t *in, size_t size, uint8_t *out);
static Buffer fromHex(const char *data, size_t size);
static void toHex(const uint8_t *in, size_t size, uint8_t *out);
String toHex() const;
private:
void copy(const char *data, size_t size);
void move(Buffer &&other);
char *m_data = nullptr;
size_t m_size = 0;
};
using Buffer = std::vector<uint8_t>;
} /* namespace xmrig */

275
src/base/tools/Cvt.cpp Normal file
View file

@ -0,0 +1,275 @@
/* XMRig
* Copyright (c) 2013-2020 Frank Denis <j at pureftpd dot org>
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "base/tools/Cvt.h"
#include "3rdparty/rapidjson/document.h"
#include <cassert>
#include <random>
#ifdef XMRIG_SODIUM
# include <sodium.h>
#endif
namespace xmrig {
#ifndef XMRIG_SODIUM
static std::random_device randomDevice;
static std::mt19937 randomEngine(randomDevice());
static char *cvt_bin2hex(char *const hex, const size_t hex_maxlen, const unsigned char *const bin, const size_t bin_len)
{
size_t i = 0U;
unsigned int x;
int b;
int c;
if (bin_len >= SIZE_MAX / 2 || hex_maxlen <= bin_len * 2U) {
return nullptr; /* LCOV_EXCL_LINE */
}
while (i < bin_len) {
c = bin[i] & 0xf;
b = bin[i] >> 4;
x = (unsigned char) (87U + c + (((c - 10U) >> 8) & ~38U)) << 8 |
(unsigned char) (87U + b + (((b - 10U) >> 8) & ~38U));
hex[i * 2U] = (char) x;
x >>= 8;
hex[i * 2U + 1U] = (char) x;
i++;
}
hex[i * 2U] = 0U;
return hex;
}
static int cvt_hex2bin(unsigned char *const bin, const size_t bin_maxlen, const char *const hex, const size_t hex_len, const char *const ignore, size_t *const bin_len, const char **const hex_end)
{
size_t bin_pos = 0U;
size_t hex_pos = 0U;
int ret = 0;
unsigned char c;
unsigned char c_acc = 0U;
unsigned char c_alpha0;
unsigned char c_alpha;
unsigned char c_num0;
unsigned char c_num;
unsigned char c_val;
unsigned char state = 0U;
while (hex_pos < hex_len) {
c = (unsigned char) hex[hex_pos];
c_num = c ^ 48U;
c_num0 = (c_num - 10U) >> 8;
c_alpha = (c & ~32U) - 55U;
c_alpha0 = ((c_alpha - 10U) ^ (c_alpha - 16U)) >> 8;
if ((c_num0 | c_alpha0) == 0U) {
if (ignore != nullptr && state == 0U && strchr(ignore, c) != nullptr) {
hex_pos++;
continue;
}
break;
}
c_val = (c_num0 & c_num) | (c_alpha0 & c_alpha);
if (bin_pos >= bin_maxlen) {
ret = -1;
errno = ERANGE;
break;
}
if (state == 0U) {
c_acc = c_val * 16U;
} else {
bin[bin_pos++] = c_acc | c_val;
}
state = ~state;
hex_pos++;
}
if (state != 0U) {
hex_pos--;
errno = EINVAL;
ret = -1;
}
if (ret != 0) {
bin_pos = 0U;
}
if (hex_end != nullptr) {
*hex_end = &hex[hex_pos];
} else if (hex_pos != hex_len) {
errno = EINVAL;
ret = -1;
}
if (bin_len != nullptr) {
*bin_len = bin_pos;
}
return ret;
}
#define sodium_bin2hex cvt_bin2hex
#define sodium_hex2bin cvt_hex2bin
#endif
template<typename T>
inline bool fromHexImpl(T &buf, const char *in, size_t size)
{
assert(in != nullptr && size > 0);
if (in == nullptr || size == 0) {
return false;
}
buf.resize(size / 2);
return sodium_hex2bin(reinterpret_cast<uint8_t *>(&buf.front()), buf.size(), in, size, nullptr, nullptr, nullptr) == 0;
}
} // namespace xmrig
bool xmrig::Cvt::fromHex(Buffer &buf, const char *in, size_t size)
{
return fromHexImpl(buf, in, size);
}
bool xmrig::Cvt::fromHex(Buffer &buf, const rapidjson::Value &value)
{
if (!value.IsString()) {
return false;
}
return fromHexImpl(buf, value.GetString(), value.GetStringLength());
}
bool xmrig::Cvt::fromHex(std::string &buf, const char *in, size_t size)
{
return fromHexImpl(buf, in, size);
}
bool xmrig::Cvt::fromHex(uint8_t *bin, size_t bin_maxlen, const char *hex, size_t hex_len)
{
assert(hex != nullptr && hex_len > 0);
if (hex == nullptr || hex_len == 0) {
return false;
}
return sodium_hex2bin(bin, bin_maxlen, hex, hex_len, nullptr, nullptr, nullptr) == 0;
}
bool xmrig::Cvt::fromHex(uint8_t *bin, size_t max, const rapidjson::Value &value)
{
if (!value.IsString()) {
return false;
}
return fromHex(bin, max, value.GetString(), value.GetStringLength());
}
xmrig::Buffer xmrig::Cvt::fromHex(const char *in, size_t size)
{
Buffer buf;
if (!fromHex(buf, in, size)) {
return {};
}
return buf;
}
bool xmrig::Cvt::toHex(char *hex, size_t hex_maxlen, const uint8_t *bin, size_t bin_len)
{
return sodium_bin2hex(hex, hex_maxlen, bin, bin_len) != nullptr;
}
xmrig::Buffer xmrig::Cvt::randomBytes(const size_t size)
{
Buffer buf(size);
# ifndef XMRIG_SODIUM
std::uniform_int_distribution<> dis(0, 255);
for (size_t i = 0; i < size; ++i) {
buf[i] = static_cast<char>(dis(randomEngine));
}
# else
randombytes_buf(buf.data(), size);
# endif
return buf;
}
rapidjson::Value xmrig::Cvt::toHex(const Buffer &data, rapidjson::Document &doc)
{
return toHex(data.data(), data.size(), doc);
}
rapidjson::Value xmrig::Cvt::toHex(const std::string &data, rapidjson::Document &doc)
{
return toHex(reinterpret_cast<const uint8_t *>(data.data()), data.size(), doc);
}
rapidjson::Value xmrig::Cvt::toHex(const uint8_t *in, size_t size, rapidjson::Document &doc)
{
return toHex(in, size).toJSON(doc);
}
xmrig::String xmrig::Cvt::toHex(const uint8_t *in, size_t size)
{
assert(in != nullptr && size > 0);
if (in == nullptr || size == 0) {
return {};
}
const size_t hex_maxlen = size * 2 + 1;
char *buf = new char[hex_maxlen];
if (!toHex(buf, hex_maxlen, in, size)) {
delete [] buf;
return {};
}
return buf;
}

61
src/base/tools/Cvt.h Normal file
View file

@ -0,0 +1,61 @@
/* XMRig
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_CVT_H
#define XMRIG_CVT_H
#include "3rdparty/rapidjson/fwd.h"
#include "base/tools/Buffer.h"
#include "base/tools/String.h"
#include <string>
namespace xmrig {
class Cvt
{
public:
inline static bool fromHex(Buffer &buf, const String &hex) { return fromHex(buf, hex.data(), hex.size()); }
inline static Buffer fromHex(const std::string &hex) { return fromHex(hex.data(), hex.size()); }
inline static Buffer fromHex(const String &hex) { return fromHex(hex.data(), hex.size()); }
inline static String toHex(const Buffer &data) { return toHex(data.data(), data.size()); }
inline static String toHex(const std::string &data) { return toHex(reinterpret_cast<const uint8_t *>(data.data()), data.size()); }
static bool fromHex(Buffer &buf, const char *in, size_t size);
static bool fromHex(Buffer &buf, const rapidjson::Value &value);
static bool fromHex(std::string &buf, const char *in, size_t size);
static bool fromHex(uint8_t *bin, size_t bin_maxlen, const char *hex, size_t hex_len);
static bool fromHex(uint8_t *bin, size_t bin_maxlen, const rapidjson::Value &value);
static bool toHex(char *hex, size_t hex_maxlen, const uint8_t *bin, size_t bin_len);
static Buffer fromHex(const char *in, size_t size);
static Buffer randomBytes(size_t size);
static rapidjson::Value toHex(const Buffer &data, rapidjson::Document &doc);
static rapidjson::Value toHex(const std::string &data, rapidjson::Document &doc);
static rapidjson::Value toHex(const uint8_t *in, size_t size, rapidjson::Document &doc);
static String toHex(const uint8_t *in, size_t size);
};
} /* namespace xmrig */
#endif /* XMRIG_CVT_H */

View file

@ -1,101 +0,0 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "base/tools/Profiler.h"
#include "base/io/log/Log.h"
#include "base/io/log/Tags.h"
#include <cstring>
#include <sstream>
#include <thread>
#include <chrono>
#include <algorithm>
#ifdef XMRIG_FEATURE_PROFILING
ProfileScopeData* ProfileScopeData::s_data[MAX_DATA_COUNT] = {};
volatile long ProfileScopeData::s_dataCount = 0;
double ProfileScopeData::s_tscSpeed = 0.0;
#ifndef NOINLINE
#ifdef __GNUC__
#define NOINLINE __attribute__ ((noinline))
#elif _MSC_VER
#define NOINLINE __declspec(noinline)
#else
#define NOINLINE
#endif
#endif
static std::string get_thread_id()
{
std::stringstream ss;
ss << std::this_thread::get_id();
std::string s = ss.str();
if (s.length() > ProfileScopeData::MAX_THREAD_ID_LENGTH) {
s.resize(ProfileScopeData::MAX_THREAD_ID_LENGTH);
}
return s;
}
NOINLINE void ProfileScopeData::Register(ProfileScopeData* data)
{
#ifdef _MSC_VER
const long id = _InterlockedIncrement(&s_dataCount) - 1;
#else
const long id = __sync_fetch_and_add(&s_dataCount, 1);
#endif
if (static_cast<unsigned long>(id) < MAX_DATA_COUNT) {
s_data[id] = data;
const std::string s = get_thread_id();
memcpy(data->m_threadId, s.c_str(), s.length() + 1);
}
}
NOINLINE void ProfileScopeData::Init()
{
using namespace std::chrono;
const uint64_t t1 = static_cast<uint64_t>(time_point_cast<nanoseconds>(high_resolution_clock::now()).time_since_epoch().count());
const uint64_t count1 = ReadTSC();
for (;;)
{
const uint64_t t2 = static_cast<uint64_t>(time_point_cast<nanoseconds>(high_resolution_clock::now()).time_since_epoch().count());
const uint64_t count2 = ReadTSC();
if (t2 - t1 > 1000000000) {
s_tscSpeed = (count2 - count1) * 1e9 / (t2 - t1);
LOG_INFO("%s TSC speed = %.3f GHz", xmrig::Tags::profiler(), s_tscSpeed / 1e9);
return;
}
}
}
#endif /* XMRIG_FEATURE_PROFILING */

View file

@ -1,133 +0,0 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_PROFILER_H
#define XMRIG_PROFILER_H
#ifndef FORCE_INLINE
#if defined(_MSC_VER)
#define FORCE_INLINE __forceinline
#elif defined(__GNUC__)
#define FORCE_INLINE __attribute__((always_inline)) inline
#elif defined(__clang__)
#define FORCE_INLINE __inline__
#else
#define FORCE_INLINE
#endif
#endif
#ifdef XMRIG_FEATURE_PROFILING
#include <cstdint>
#include <cstddef>
#include <type_traits>
#if defined(_MSC_VER)
#include <intrin.h>
#endif
static FORCE_INLINE uint64_t ReadTSC()
{
#ifdef _MSC_VER
return __rdtsc();
#else
uint32_t hi, lo;
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
return (((uint64_t)hi) << 32) | lo;
#endif
}
struct ProfileScopeData
{
const char* m_name;
uint64_t m_totalCycles;
uint32_t m_totalSamples;
enum
{
MAX_THREAD_ID_LENGTH = 11,
MAX_SAMPLE_COUNT = 128,
MAX_DATA_COUNT = 1024
};
char m_threadId[MAX_THREAD_ID_LENGTH + 1];
static ProfileScopeData* s_data[MAX_DATA_COUNT];
static volatile long s_dataCount;
static double s_tscSpeed;
static void Register(ProfileScopeData* data);
static void Init();
};
static_assert(std::is_trivial<ProfileScopeData>::value, "ProfileScopeData must be a trivial struct");
static_assert(sizeof(ProfileScopeData) <= 32, "ProfileScopeData struct is too big");
class ProfileScope
{
public:
FORCE_INLINE ProfileScope(ProfileScopeData& data)
: m_data(data)
{
if (m_data.m_totalCycles == 0) {
ProfileScopeData::Register(&data);
}
m_startCounter = ReadTSC();
}
FORCE_INLINE ~ProfileScope()
{
m_data.m_totalCycles += ReadTSC() - m_startCounter;
++m_data.m_totalSamples;
}
private:
ProfileScopeData& m_data;
uint64_t m_startCounter;
};
#define PROFILE_SCOPE(x) static thread_local ProfileScopeData x##_data{#x}; ProfileScope x(x##_data);
#else /* XMRIG_FEATURE_PROFILING */
#define PROFILE_SCOPE(x)
#endif /* XMRIG_FEATURE_PROFILING */
#include "crypto/randomx/blake2/blake2.h"
struct rx_blake2b_wrapper
{
FORCE_INLINE static void run(void* out, size_t outlen, const void* in, size_t inlen)
{
PROFILE_SCOPE(RandomX_Blake2b);
rx_blake2b(out, outlen, in, inlen);
}
};
#endif /* XMRIG_PROFILER_H */

View file

@ -125,7 +125,7 @@ std::vector<xmrig::String> xmrig::String::split(char sep) const
for (pos = 0; pos < m_size; ++pos) {
if (m_data[pos] == sep) {
if ((pos - start) > 0) {
if (pos > start) {
out.emplace_back(m_data + start, pos - start);
}
@ -133,7 +133,7 @@ std::vector<xmrig::String> xmrig::String::split(char sep) const
}
}
if ((pos - start) > 0) {
if (pos > start) {
out.emplace_back(m_data + start, pos - start);
}