From d8f39ebef35d1f1ba9f44cda6a88f8fce1528f9b Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 13 Jul 2022 17:45:18 +0700 Subject: [PATCH] Update DNS subsystem. --- src/base/base.cmake | 8 +- src/base/kernel/config/BaseConfig.cpp | 4 +- src/base/kernel/config/BaseTransform.cpp | 6 +- src/base/kernel/config/BaseTransform.h | 4 +- src/base/kernel/private/DnsConfig.cpp | 107 ++++++++++++++++++ .../{net/dns => kernel/private}/DnsConfig.h | 32 ++++-- src/base/net/dns/Dns.cpp | 11 +- src/base/net/dns/Dns.h | 20 +--- src/base/net/dns/DnsConfig.cpp | 56 --------- src/base/net/dns/DnsRecords.cpp | 4 +- src/core/config/Config.cpp | 7 +- src/core/config/Config.h | 4 +- 12 files changed, 159 insertions(+), 104 deletions(-) create mode 100644 src/base/kernel/private/DnsConfig.cpp rename src/base/{net/dns => kernel/private}/DnsConfig.h (53%) delete mode 100644 src/base/net/dns/DnsConfig.cpp diff --git a/src/base/base.cmake b/src/base/base.cmake index bf89391c..857e2896 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -21,7 +21,6 @@ set(HEADERS_BASE src/base/kernel/Base.h src/base/kernel/config/BaseConfig.h src/base/kernel/config/BaseTransform.h - src/base/kernel/private/Title.h src/base/kernel/constants.h src/base/kernel/Entry.h src/base/kernel/interfaces/IAsyncListener.h @@ -43,10 +42,11 @@ set(HEADERS_BASE src/base/kernel/interfaces/IWatcherListener.h src/base/kernel/Lib.h src/base/kernel/OS.h + src/base/kernel/private/DnsConfig.h + src/base/kernel/private/Title.h src/base/kernel/Process.h src/base/kernel/Versions.h src/base/net/dns/Dns.h - src/base/net/dns/DnsConfig.h src/base/net/dns/DnsRecord.h src/base/net/dns/DnsRecords.h src/base/net/dns/DnsRequest.h @@ -111,14 +111,14 @@ set(SOURCES_BASE src/base/kernel/Base.cpp src/base/kernel/config/BaseConfig.cpp src/base/kernel/config/BaseTransform.cpp - src/base/kernel/private/Title.cpp src/base/kernel/Entry.cpp src/base/kernel/Lib.cpp src/base/kernel/OS.cpp + src/base/kernel/private/DnsConfig.cpp + src/base/kernel/private/Title.cpp src/base/kernel/Process.cpp src/base/kernel/Versions.cpp src/base/net/dns/Dns.cpp - src/base/net/dns/DnsConfig.cpp src/base/net/dns/DnsRecord.cpp src/base/net/dns/DnsRecords.cpp src/base/net/dns/DnsUvBackend.cpp diff --git a/src/base/kernel/config/BaseConfig.cpp b/src/base/kernel/config/BaseConfig.cpp index d3ad5d06..a4a60733 100644 --- a/src/base/kernel/config/BaseConfig.cpp +++ b/src/base/kernel/config/BaseConfig.cpp @@ -30,9 +30,9 @@ #include "base/io/log/Log.h" #include "base/io/log/Tags.h" #include "base/kernel/interfaces/IJsonReader.h" +#include "base/kernel/private/DnsConfig.h" #include "base/kernel/Process.h" #include "base/kernel/Versions.h" -#include "base/net/dns/Dns.h" #include "version.h" @@ -114,7 +114,7 @@ bool xmrig::BaseConfig::read(const IJsonReader &reader, const char *fileName) m_http.load(reader.getObject(kHttp)); m_pools.load(reader); - Dns::set(reader.getObject(DnsConfig::kField)); + DnsConfig::set(reader.getObject(DnsConfig::kField)); return m_pools.active() > 0; } diff --git a/src/base/kernel/config/BaseTransform.cpp b/src/base/kernel/config/BaseTransform.cpp index b1d8201d..1e900b97 100644 --- a/src/base/kernel/config/BaseTransform.cpp +++ b/src/base/kernel/config/BaseTransform.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,8 +31,8 @@ #include "base/io/log/Log.h" #include "base/kernel/config/BaseConfig.h" #include "base/kernel/interfaces/IConfig.h" +#include "base/kernel/private/DnsConfig.h" #include "base/kernel/Process.h" -#include "base/net/dns/DnsConfig.h" #include "base/net/stratum/Pool.h" #include "base/net/stratum/Pools.h" #include "base/tools/Arguments.h" diff --git a/src/base/kernel/config/BaseTransform.h b/src/base/kernel/config/BaseTransform.h index 0ddcebc8..dab4cfcb 100644 --- a/src/base/kernel/config/BaseTransform.h +++ b/src/base/kernel/config/BaseTransform.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/base/kernel/private/DnsConfig.cpp b/src/base/kernel/private/DnsConfig.cpp new file mode 100644 index 00000000..bd37e301 --- /dev/null +++ b/src/base/kernel/private/DnsConfig.cpp @@ -0,0 +1,107 @@ +/* XMRig + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "base/kernel/private/DnsConfig.h" +#include "3rdparty/rapidjson/document.h" +#include "base/io/json/Json.h" +#include "base/tools/Arguments.h" + + +#ifdef APP_DEBUG +# include "base/io/log/Log.h" +# include "base/kernel/Config.h" +#endif + + +#include + + +namespace xmrig { + + +const char *DnsConfig::kField = "dns"; +const char *DnsConfig::kIPv6 = "ipv6"; +const char *DnsConfig::kTTL = "ttl"; + + +#ifndef XMRIG_FEATURE_EVENTS +static DnsConfig config; +#endif + + +} // namespace xmrig + + +xmrig::DnsConfig::DnsConfig(const Arguments &arguments) : + m_ipv6(arguments.contains("--dns-ipv6")), + m_ttl(std::max(arguments.value("--dns-ttl").toUint(kDefaultTTL), 1U)) +{ +} + + +xmrig::DnsConfig::DnsConfig(const rapidjson::Value &value, const DnsConfig ¤t) +{ + m_ipv6 = Json::getBool(value, kIPv6, current.m_ipv6); + m_ttl = std::max(Json::getUint(value, kTTL, current.m_ttl), 1U); +} + + +bool xmrig::DnsConfig::isEqual(const DnsConfig &other) const +{ + return other.m_ipv6 == m_ipv6 && other.m_ttl == m_ttl; +} + + +rapidjson::Value xmrig::DnsConfig::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + + auto &allocator = doc.GetAllocator(); + Value obj(kObjectType); + + obj.AddMember(StringRef(kIPv6), m_ipv6 ? Value(m_ipv6) : Value(kNullType), allocator); + obj.AddMember(StringRef(kTTL), m_ttl == kDefaultTTL ? Value(kNullType) : Value(m_ttl), allocator); + + return obj; +} + + +void xmrig::DnsConfig::print() const +{ +# ifdef APP_DEBUG + LOG_DEBUG("%s " MAGENTA_BOLD("DNS") + MAGENTA(""), + Config::tag(), m_ipv6, m_ttl); +# endif +} + + +#ifndef XMRIG_FEATURE_EVENTS +const xmrig::DnsConfig &xmrig::DnsConfig::current() +{ + return config; +} + + +void xmrig::DnsConfig::set(const rapidjson::Value &value) +{ + config = { value, config }; +} +#endif diff --git a/src/base/net/dns/DnsConfig.h b/src/base/kernel/private/DnsConfig.h similarity index 53% rename from src/base/net/dns/DnsConfig.h rename to src/base/kernel/private/DnsConfig.h index 605e837b..aa321ed8 100644 --- a/src/base/net/dns/DnsConfig.h +++ b/src/base/kernel/private/DnsConfig.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,6 +26,9 @@ namespace xmrig { +class Arguments; + + class DnsConfig { public: @@ -33,22 +36,35 @@ public: static const char *kIPv6; static const char *kTTL; + static constexpr uint32_t kDefaultTTL = 30U; + DnsConfig() = default; - DnsConfig(const rapidjson::Value &value); + DnsConfig(const Arguments &arguments); + DnsConfig(const rapidjson::Value &value, const DnsConfig ¤t); - inline bool isIPv6() const { return m_ipv6; } - inline uint32_t ttl() const { return m_ttl * 1000U; } + inline bool isIPv6() const { return m_ipv6; } + inline uint32_t ttl() const { return m_ttl * 1000U; } + inline bool operator!=(const DnsConfig &other) const { return !isEqual(other); } + inline bool operator==(const DnsConfig &other) const { return isEqual(other); } + + bool isEqual(const DnsConfig &other) const; rapidjson::Value toJSON(rapidjson::Document &doc) const; + void print() const; + static const DnsConfig ¤t(); + +# ifndef XMRIG_FEATURE_EVENTS + static void set(const rapidjson::Value &value); +# endif private: bool m_ipv6 = false; - uint32_t m_ttl = 30U; + uint32_t m_ttl = kDefaultTTL; }; -} /* namespace xmrig */ +} // namespace xmrig -#endif /* XMRIG_DNSCONFIG_H */ +#endif // XMRIG_DNSCONFIG_H diff --git a/src/base/net/dns/Dns.cpp b/src/base/net/dns/Dns.cpp index 85590bb7..b71f516b 100644 --- a/src/base/net/dns/Dns.cpp +++ b/src/base/net/dns/Dns.cpp @@ -16,16 +16,15 @@ * along with this program. If not, see . */ - #include "base/net/dns/Dns.h" +#include "base/kernel/private/DnsConfig.h" #include "base/net/dns/DnsUvBackend.h" namespace xmrig { -DnsConfig Dns::m_config; -std::map > Dns::m_backends; +static std::map > backends; } // namespace xmrig @@ -33,9 +32,9 @@ std::map > Dns::m_backends; std::shared_ptr xmrig::Dns::resolve(const String &host, IDnsListener *listener, uint64_t ttl) { - if (m_backends.find(host) == m_backends.end()) { - m_backends.insert({ host, std::make_shared() }); + if (backends.find(host) == backends.end()) { + backends.insert({ host, std::make_shared() }); } - return m_backends.at(host)->resolve(host, listener, ttl == 0 ? m_config.ttl() : ttl); + return backends.at(host)->resolve(host, listener, ttl == 0 ? DnsConfig::current().ttl() : ttl); } diff --git a/src/base/net/dns/Dns.h b/src/base/net/dns/Dns.h index cf054390..30b45932 100644 --- a/src/base/net/dns/Dns.h +++ b/src/base/net/dns/Dns.h @@ -20,38 +20,26 @@ #define XMRIG_DNS_H -#include "base/net/dns/DnsConfig.h" -#include "base/tools/String.h" - - -#include +#include #include namespace xmrig { -class DnsConfig; class DnsRequest; -class IDnsBackend; class IDnsListener; +class String; class Dns { public: - inline static const DnsConfig &config() { return m_config; } - inline static void set(const DnsConfig &config) { m_config = config; } - static std::shared_ptr resolve(const String &host, IDnsListener *listener, uint64_t ttl = 0); - -private: - static DnsConfig m_config; - static std::map > m_backends; }; -} /* namespace xmrig */ +} // namespace xmrig -#endif /* XMRIG_DNS_H */ +#endif // XMRIG_DNS_H diff --git a/src/base/net/dns/DnsConfig.cpp b/src/base/net/dns/DnsConfig.cpp deleted file mode 100644 index f9ec7e28..00000000 --- a/src/base/net/dns/DnsConfig.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "base/net/dns/DnsConfig.h" -#include "3rdparty/rapidjson/document.h" -#include "base/io/json/Json.h" - - -#include - - -namespace xmrig { - - -const char *DnsConfig::kField = "dns"; -const char *DnsConfig::kIPv6 = "ipv6"; -const char *DnsConfig::kTTL = "ttl"; - - -} // namespace xmrig - - -xmrig::DnsConfig::DnsConfig(const rapidjson::Value &value) -{ - m_ipv6 = Json::getBool(value, kIPv6, m_ipv6); - m_ttl = std::max(Json::getUint(value, kTTL, m_ttl), 1U); -} - - -rapidjson::Value xmrig::DnsConfig::toJSON(rapidjson::Document &doc) const -{ - using namespace rapidjson; - - auto &allocator = doc.GetAllocator(); - Value obj(kObjectType); - - obj.AddMember(StringRef(kIPv6), m_ipv6, allocator); - obj.AddMember(StringRef(kTTL), m_ttl, allocator); - - return obj; -} diff --git a/src/base/net/dns/DnsRecords.cpp b/src/base/net/dns/DnsRecords.cpp index 072f9672..20a78d88 100644 --- a/src/base/net/dns/DnsRecords.cpp +++ b/src/base/net/dns/DnsRecords.cpp @@ -20,7 +20,7 @@ #include "base/net/dns/DnsRecords.h" -#include "base/net/dns/Dns.h" +#include "base/kernel/private/DnsConfig.h" const xmrig::DnsRecord &xmrig::DnsRecords::get(DnsRecord::Type prefered) const @@ -34,7 +34,7 @@ const xmrig::DnsRecord &xmrig::DnsRecords::get(DnsRecord::Type prefered) const const size_t ipv4 = m_ipv4.size(); const size_t ipv6 = m_ipv6.size(); - if (ipv6 && (prefered == DnsRecord::AAAA || Dns::config().isIPv6() || !ipv4)) { + if (ipv6 && (prefered == DnsRecord::AAAA || DnsConfig::current().isIPv6() || !ipv4)) { return m_ipv6[ipv6 == 1 ? 0 : static_cast(rand()) % ipv6]; // NOLINT(concurrency-mt-unsafe, cert-msc30-c, cert-msc50-cpp) } diff --git a/src/core/config/Config.cpp b/src/core/config/Config.cpp index 6150607d..ee883325 100644 --- a/src/core/config/Config.cpp +++ b/src/core/config/Config.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,6 +27,7 @@ #include "backend/cpu/Cpu.h" #include "base/io/log/Log.h" #include "base/kernel/interfaces/IJsonReader.h" +#include "base/kernel/private/DnsConfig.h" #include "base/net/dns/Dns.h" #include "crypto/common/Assembly.h" @@ -299,7 +300,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember(StringRef(kTls), m_tls.toJSON(doc), allocator); # endif - doc.AddMember(StringRef(DnsConfig::kField), Dns::config().toJSON(doc), allocator); + doc.AddMember(StringRef(DnsConfig::kField), DnsConfig::current().toJSON(doc), allocator); doc.AddMember(StringRef(kUserAgent), m_userAgent.toJSON(), allocator); doc.AddMember(StringRef(kVerbose), Log::verbose(), allocator); doc.AddMember(StringRef(kWatch), m_watch, allocator); diff --git a/src/core/config/Config.h b/src/core/config/Config.h index c2da8523..0f9cf54d 100644 --- a/src/core/config/Config.h +++ b/src/core/config/Config.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by