diff --git a/src/base/kernel/config/BaseTransform.cpp b/src/base/kernel/config/BaseTransform.cpp index a1b430da..339e4621 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-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -18,14 +18,12 @@ #include - #ifdef _MSC_VER # include "getopt/getopt.h" #else # include #endif - #include "base/kernel/config/BaseTransform.h" #include "base/io/json/JsonChain.h" #include "base/io/log/Log.h" @@ -37,7 +35,6 @@ #include "base/net/stratum/Pools.h" #include "core/config/Config_platform.h" - #ifdef XMRIG_FEATURE_TLS # include "base/net/tls/TlsConfig.h" #endif @@ -47,9 +44,9 @@ void xmrig::BaseTransform::load(JsonChain &chain, Process *process, IConfigTrans { using namespace rapidjson; - int key = 0; - int argc = process->arguments().argc(); - char **argv = process->arguments().argv(); + int key = 0; + const int argc = process->arguments().argc(); + char **argv = process->arguments().argv(); Document doc(kObjectType); @@ -262,7 +259,8 @@ void xmrig::BaseTransform::transform(rapidjson::Document &doc, int key, const ch case IConfig::DaemonKey: /* --daemon */ case IConfig::SubmitToOriginKey: /* --submit-to-origin */ case IConfig::VerboseKey: /* --verbose */ - case IConfig::DnsIPv6Key: /* --dns-ipv6 */ + case IConfig::DnsIPv4Key: /* --ipv4 */ + case IConfig::DnsIPv6Key: /* --ipv6 */ return transformBoolean(doc, key, true); case IConfig::ColorKey: /* --no-color */ @@ -323,8 +321,11 @@ void xmrig::BaseTransform::transformBoolean(rapidjson::Document &doc, int key, b case IConfig::NoTitleKey: /* --no-title */ return set(doc, BaseConfig::kTitle, enable); - case IConfig::DnsIPv6Key: /* --dns-ipv6 */ - return set(doc, DnsConfig::kField, DnsConfig::kIPv6, enable); + case IConfig::DnsIPv4Key: /* --ipv4 */ + return set(doc, DnsConfig::kField, DnsConfig::kIPv, 4); + + case IConfig::DnsIPv6Key: /* --ipv6 */ + return set(doc, DnsConfig::kField, DnsConfig::kIPv, 6); default: break; diff --git a/src/base/kernel/interfaces/IConfig.h b/src/base/kernel/interfaces/IConfig.h index 98957fc4..94c5760d 100644 --- a/src/base/kernel/interfaces/IConfig.h +++ b/src/base/kernel/interfaces/IConfig.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -16,9 +16,7 @@ * along with this program. If not, see . */ -#ifndef XMRIG_ICONFIG_H -#define XMRIG_ICONFIG_H - +#pragma once #include "3rdparty/rapidjson/fwd.h" @@ -82,7 +80,8 @@ public: HugePageSizeKey = 1050, PauseOnActiveKey = 1051, SubmitToOriginKey = 1052, - DnsIPv6Key = 1053, + DnsIPv4Key = '4', + DnsIPv6Key = '6', DnsTtlKey = 1054, SpendSecretKey = 1055, DaemonZMQPortKey = 1056, @@ -177,7 +176,4 @@ public: }; -} /* namespace xmrig */ - - -#endif // XMRIG_ICONFIG_H +} // namespace xmrig diff --git a/src/base/kernel/interfaces/IDnsBackend.h b/src/base/kernel/interfaces/IDnsBackend.h index ca676f32..924cf6a5 100644 --- a/src/base/kernel/interfaces/IDnsBackend.h +++ b/src/base/kernel/interfaces/IDnsBackend.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -16,21 +16,16 @@ * along with this program. If not, see . */ -#ifndef XMRIG_IDNSBACKEND_H -#define XMRIG_IDNSBACKEND_H - +#pragma once #include "base/tools/Object.h" -#include - - namespace xmrig { +class DnsConfig; class DnsRecords; -class DnsRequest; class IDnsListener; class String; @@ -43,12 +38,9 @@ public: IDnsBackend() = default; virtual ~IDnsBackend() = default; - virtual const DnsRecords &records() const = 0; - virtual std::shared_ptr resolve(const String &host, IDnsListener *listener, uint64_t ttl) = 0; + virtual const DnsRecords &records() const = 0; + virtual void resolve(const String &host, const std::weak_ptr &listener, const DnsConfig &config) = 0; }; -} /* namespace xmrig */ - - -#endif // XMRIG_IDNSBACKEND_H +} // namespace xmrig diff --git a/src/base/net/dns/Dns.cpp b/src/base/net/dns/Dns.cpp index 85590bb7..d5e71b49 100644 --- a/src/base/net/dns/Dns.cpp +++ b/src/base/net/dns/Dns.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -18,6 +18,7 @@ #include "base/net/dns/Dns.h" +#include "base/net/dns/DnsRequest.h" #include "base/net/dns/DnsUvBackend.h" @@ -25,17 +26,21 @@ namespace xmrig { DnsConfig Dns::m_config; -std::map > Dns::m_backends; +std::map> Dns::m_backends; } // namespace xmrig -std::shared_ptr xmrig::Dns::resolve(const String &host, IDnsListener *listener, uint64_t ttl) +std::shared_ptr xmrig::Dns::resolve(const String &host, IDnsListener *listener) { + auto req = std::make_shared(listener); + if (m_backends.find(host) == m_backends.end()) { m_backends.insert({ host, std::make_shared() }); } - return m_backends.at(host)->resolve(host, listener, ttl == 0 ? m_config.ttl() : ttl); + m_backends.at(host)->resolve(host, req, m_config); + + return req; } diff --git a/src/base/net/dns/Dns.h b/src/base/net/dns/Dns.h index cf054390..2408245f 100644 --- a/src/base/net/dns/Dns.h +++ b/src/base/net/dns/Dns.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -43,7 +43,7 @@ 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); + static std::shared_ptr resolve(const String &host, IDnsListener *listener); private: static DnsConfig m_config; diff --git a/src/base/net/dns/DnsConfig.cpp b/src/base/net/dns/DnsConfig.cpp index f9ec7e28..3b2bbe73 100644 --- a/src/base/net/dns/DnsConfig.cpp +++ b/src/base/net/dns/DnsConfig.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -20,15 +20,15 @@ #include "3rdparty/rapidjson/document.h" #include "base/io/json/Json.h" - #include +#include namespace xmrig { const char *DnsConfig::kField = "dns"; -const char *DnsConfig::kIPv6 = "ipv6"; +const char *DnsConfig::kIPv = "ip_version"; const char *DnsConfig::kTTL = "ttl"; @@ -37,8 +37,26 @@ const char *DnsConfig::kTTL = "ttl"; 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); + const uint32_t ipv = Json::getUint(value, kIPv, m_ipv); + if (ipv == 0 || ipv == 4 || ipv == 6) { + m_ipv = ipv; + } + + m_ttl = std::max(Json::getUint(value, kTTL, m_ttl), 1U); +} + + +int xmrig::DnsConfig::ai_family() const +{ + if (m_ipv == 4) { + return AF_INET; + } + + if (m_ipv == 6) { + return AF_INET6; + } + + return AF_UNSPEC; } @@ -49,8 +67,8 @@ rapidjson::Value xmrig::DnsConfig::toJSON(rapidjson::Document &doc) const auto &allocator = doc.GetAllocator(); Value obj(kObjectType); - obj.AddMember(StringRef(kIPv6), m_ipv6, allocator); - obj.AddMember(StringRef(kTTL), m_ttl, allocator); + obj.AddMember(StringRef(kIPv), m_ipv, allocator); + obj.AddMember(StringRef(kTTL), m_ttl, allocator); return obj; } diff --git a/src/base/net/dns/DnsConfig.h b/src/base/net/dns/DnsConfig.h index 605e837b..7c985f90 100644 --- a/src/base/net/dns/DnsConfig.h +++ b/src/base/net/dns/DnsConfig.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -16,9 +16,7 @@ * along with this program. If not, see . */ -#ifndef XMRIG_DNSCONFIG_H -#define XMRIG_DNSCONFIG_H - +#pragma once #include "3rdparty/rapidjson/fwd.h" @@ -30,25 +28,22 @@ class DnsConfig { public: static const char *kField; - static const char *kIPv6; + static const char *kIPv; static const char *kTTL; DnsConfig() = default; DnsConfig(const rapidjson::Value &value); - inline bool isIPv6() const { return m_ipv6; } + inline uint32_t ipv() const { return m_ipv; } inline uint32_t ttl() const { return m_ttl * 1000U; } + int ai_family() const; rapidjson::Value toJSON(rapidjson::Document &doc) const; - private: - bool m_ipv6 = false; - uint32_t m_ttl = 30U; + uint32_t m_ttl = 30U; + uint32_t m_ipv = 0U; }; -} /* namespace xmrig */ - - -#endif /* XMRIG_DNSCONFIG_H */ +} // namespace xmrig diff --git a/src/base/net/dns/DnsRecord.cpp b/src/base/net/dns/DnsRecord.cpp index 3bf8f097..2b5a5058 100644 --- a/src/base/net/dns/DnsRecord.cpp +++ b/src/base/net/dns/DnsRecord.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2023 SChernykh - * Copyright (c) 2016-2023 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -16,19 +16,16 @@ * along with this program. If not, see . */ - #include - #include "base/net/dns/DnsRecord.h" -xmrig::DnsRecord::DnsRecord(const addrinfo *addr) : - m_type(addr->ai_family == AF_INET6 ? AAAA : (addr->ai_family == AF_INET ? A : Unknown)) +xmrig::DnsRecord::DnsRecord(const addrinfo *addr) { static_assert(sizeof(m_data) >= sizeof(sockaddr_in6), "Not enough storage for IPv6 address."); - memcpy(m_data, addr->ai_addr, m_type == AAAA ? sizeof(sockaddr_in6) : sizeof(sockaddr_in)); + memcpy(m_data, addr->ai_addr, addr->ai_family == AF_INET6 ? sizeof(sockaddr_in6) : sizeof(sockaddr_in)); } @@ -44,7 +41,7 @@ xmrig::String xmrig::DnsRecord::ip() const { char *buf = nullptr; - if (m_type == AAAA) { + if (reinterpret_cast(m_data).sa_family == AF_INET6) { buf = new char[45](); uv_ip6_name(reinterpret_cast(m_data), buf, 45); } diff --git a/src/base/net/dns/DnsRecord.h b/src/base/net/dns/DnsRecord.h index 7a68ea2f..548b603f 100644 --- a/src/base/net/dns/DnsRecord.h +++ b/src/base/net/dns/DnsRecord.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -16,14 +16,11 @@ * along with this program. If not, see . */ -#ifndef XMRIG_DNSRECORD_H -#define XMRIG_DNSRECORD_H - +#pragma once struct addrinfo; struct sockaddr; - #include "base/tools/String.h" @@ -33,28 +30,15 @@ namespace xmrig { class DnsRecord { public: - enum Type : uint32_t { - Unknown, - A, - AAAA - }; - DnsRecord() {} DnsRecord(const addrinfo *addr); const sockaddr *addr(uint16_t port = 0) const; String ip() const; - inline bool isValid() const { return m_type != Unknown; } - inline Type type() const { return m_type; } - private: mutable uint8_t m_data[28]{}; - const Type m_type = Unknown; }; -} /* namespace xmrig */ - - -#endif /* XMRIG_DNSRECORD_H */ +} // namespace xmrig diff --git a/src/base/net/dns/DnsRecords.cpp b/src/base/net/dns/DnsRecords.cpp index 072f9672..aff2b323 100644 --- a/src/base/net/dns/DnsRecords.cpp +++ b/src/base/net/dns/DnsRecords.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -18,90 +18,96 @@ #include - #include "base/net/dns/DnsRecords.h" -#include "base/net/dns/Dns.h" -const xmrig::DnsRecord &xmrig::DnsRecords::get(DnsRecord::Type prefered) const +namespace { + + +static size_t dns_records_count(const addrinfo *res, int &ai_family) +{ + size_t ipv4 = 0; + size_t ipv6 = 0; + + while (res != nullptr) { + if (res->ai_family == AF_INET) { + ++ipv4; + } + + if (res->ai_family == AF_INET6) { + ++ipv6; + } + + res = res->ai_next; + } + + if (ai_family == AF_INET6 && !ipv6) { + ai_family = AF_INET; + } + + switch (ai_family) { + case AF_UNSPEC: + return ipv4 + ipv6; + + case AF_INET: + return ipv4; + + case AF_INET6: + return ipv6; + + default: + break; + } + + return 0; +} + + +} // namespace + + +xmrig::DnsRecords::DnsRecords(const addrinfo *res, int ai_family) +{ + size_t size = dns_records_count(res, ai_family); + if (!size) { + return; + } + + m_records.reserve(size); + + if (ai_family == AF_UNSPEC) { + while (res != nullptr) { + if (res->ai_family == AF_INET || res->ai_family == AF_INET6) { + m_records.emplace_back(res); + } + + res = res->ai_next; + }; + } else { + while (res != nullptr) { + if (res->ai_family == ai_family) { + m_records.emplace_back(res); + } + + res = res->ai_next; + }; + } + + size = m_records.size(); + if (size > 1) { + m_index = static_cast(rand()) % size; // NOLINT(concurrency-mt-unsafe, cert-msc30-c, cert-msc50-cpp) + } +} + + +const xmrig::DnsRecord &xmrig::DnsRecords::get() const { static const DnsRecord defaultRecord; - if (isEmpty()) { - return defaultRecord; - } - - const size_t ipv4 = m_ipv4.size(); - const size_t ipv6 = m_ipv6.size(); - - if (ipv6 && (prefered == DnsRecord::AAAA || Dns::config().isIPv6() || !ipv4)) { - return m_ipv6[ipv6 == 1 ? 0 : static_cast(rand()) % ipv6]; // NOLINT(concurrency-mt-unsafe, cert-msc30-c, cert-msc50-cpp) - } - - if (ipv4) { - return m_ipv4[ipv4 == 1 ? 0 : static_cast(rand()) % ipv4]; // NOLINT(concurrency-mt-unsafe, cert-msc30-c, cert-msc50-cpp) + const size_t size = m_records.size(); + if (size > 0) { + return m_records[m_index++ % size]; } return defaultRecord; } - - -size_t xmrig::DnsRecords::count(DnsRecord::Type type) const -{ - if (type == DnsRecord::A) { - return m_ipv4.size(); - } - - if (type == DnsRecord::AAAA) { - return m_ipv6.size(); - } - - return m_ipv4.size() + m_ipv6.size(); -} - - -void xmrig::DnsRecords::clear() -{ - m_ipv4.clear(); - m_ipv6.clear(); -} - - -void xmrig::DnsRecords::parse(addrinfo *res) -{ - clear(); - - addrinfo *ptr = res; - size_t ipv4 = 0; - size_t ipv6 = 0; - - while (ptr != nullptr) { - if (ptr->ai_family == AF_INET) { - ++ipv4; - } - else if (ptr->ai_family == AF_INET6) { - ++ipv6; - } - - ptr = ptr->ai_next; - } - - if (ipv4 == 0 && ipv6 == 0) { - return; - } - - m_ipv4.reserve(ipv4); - m_ipv6.reserve(ipv6); - - ptr = res; - while (ptr != nullptr) { - if (ptr->ai_family == AF_INET) { - m_ipv4.emplace_back(ptr); - } - else if (ptr->ai_family == AF_INET6) { - m_ipv6.emplace_back(ptr); - } - - ptr = ptr->ai_next; - } -} diff --git a/src/base/net/dns/DnsRecords.h b/src/base/net/dns/DnsRecords.h index cfa19217..4b16d54a 100644 --- a/src/base/net/dns/DnsRecords.h +++ b/src/base/net/dns/DnsRecords.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -16,9 +16,7 @@ * along with this program. If not, see . */ -#ifndef XMRIG_DNSRECORDS_H -#define XMRIG_DNSRECORDS_H - +#pragma once #include "base/net/dns/DnsRecord.h" @@ -29,20 +27,19 @@ namespace xmrig { class DnsRecords { public: - inline bool isEmpty() const { return m_ipv4.empty() && m_ipv6.empty(); } + DnsRecords() = default; + DnsRecords(const addrinfo *res, int ai_family); - const DnsRecord &get(DnsRecord::Type prefered = DnsRecord::Unknown) const; - size_t count(DnsRecord::Type type = DnsRecord::Unknown) const; - void clear(); - void parse(addrinfo *res); + inline bool isEmpty() const { return m_records.empty(); } + inline const std::vector &records() const { return m_records; } + inline size_t size() const { return m_records.size(); } + + const DnsRecord &get() const; private: - std::vector m_ipv4; - std::vector m_ipv6; + mutable size_t m_index = 0; + std::vector m_records; }; -} /* namespace xmrig */ - - -#endif /* XMRIG_DNSRECORDS_H */ +} // namespace xmrig diff --git a/src/base/net/dns/DnsRequest.h b/src/base/net/dns/DnsRequest.h index 036eaa34..175b9c07 100644 --- a/src/base/net/dns/DnsRequest.h +++ b/src/base/net/dns/DnsRequest.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -16,35 +16,30 @@ * along with this program. If not, see . */ -#ifndef XMRIG_DNSREQUEST_H -#define XMRIG_DNSREQUEST_H +#pragma once - -#include "base/tools/Object.h" - - -#include +#include "base/kernel/interfaces/IDnsListener.h" namespace xmrig { -class IDnsListener; - - -class DnsRequest +class DnsRequest : public IDnsListener { public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(DnsRequest) - DnsRequest(IDnsListener *listener) : listener(listener) {} - ~DnsRequest() = default; + inline DnsRequest(IDnsListener *listener) : m_listener(listener) {} + ~DnsRequest() override = default; - IDnsListener *listener; +protected: + inline void onResolved(const DnsRecords &records, int status, const char *error) override { + m_listener->onResolved(records, status, error); + } + +private: + IDnsListener *m_listener; }; -} /* namespace xmrig */ - - -#endif /* XMRIG_DNSREQUEST_H */ +} // namespace xmrig diff --git a/src/base/net/dns/DnsUvBackend.cpp b/src/base/net/dns/DnsUvBackend.cpp index 33f27f95..850f493e 100644 --- a/src/base/net/dns/DnsUvBackend.cpp +++ b/src/base/net/dns/DnsUvBackend.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2023 SChernykh - * Copyright (c) 2016-2023 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -16,13 +16,11 @@ * along with this program. If not, see . */ - #include - #include "base/net/dns/DnsUvBackend.h" #include "base/kernel/interfaces/IDnsListener.h" -#include "base/net/dns/DnsRequest.h" +#include "base/net/dns/DnsConfig.h" #include "base/tools/Chrono.h" @@ -73,21 +71,23 @@ xmrig::DnsUvBackend::~DnsUvBackend() } -std::shared_ptr xmrig::DnsUvBackend::resolve(const String &host, IDnsListener *listener, uint64_t ttl) +void xmrig::DnsUvBackend::resolve(const String &host, const std::weak_ptr &listener, const DnsConfig &config) { - auto req = std::make_shared(listener); + m_queue.emplace_back(listener); - if (Chrono::currentMSecsSinceEpoch() - m_ts <= ttl && !m_records.isEmpty()) { - req->listener->onResolved(m_records, 0, nullptr); - } else { - m_queue.emplace(req); + if (Chrono::currentMSecsSinceEpoch() - m_ts <= config.ttl()) { + return notify(); } - if (m_queue.size() == 1 && !resolve(host)) { - done(); + if (m_req) { + return; } - return req; + m_ai_family = config.ai_family(); + + if (!resolve(host)) { + notify(); + } } @@ -102,44 +102,46 @@ bool xmrig::DnsUvBackend::resolve(const String &host) } -void xmrig::DnsUvBackend::done() +void xmrig::DnsUvBackend::notify() { const char *error = m_status < 0 ? uv_strerror(m_status) : nullptr; - while (!m_queue.empty()) { - auto req = std::move(m_queue.front()).lock(); - if (req) { - req->listener->onResolved(m_records, m_status, error); + for (const auto &l : m_queue) { + auto listener = l.lock(); + if (listener) { + listener->onResolved(m_records, m_status, error); } - - m_queue.pop(); } + m_queue.clear(); m_req.reset(); } void xmrig::DnsUvBackend::onResolved(int status, addrinfo *res) { - m_ts = Chrono::currentMSecsSinceEpoch(); + m_status = status; + m_ts = Chrono::currentMSecsSinceEpoch(); - if ((m_status = status) < 0) { - return done(); + if (m_status < 0) { + m_records = {}; + + return notify(); } - m_records.parse(res); + m_records = { res, m_ai_family }; if (m_records.isEmpty()) { m_status = UV_EAI_NONAME; } - done(); + notify(); } void xmrig::DnsUvBackend::onResolved(uv_getaddrinfo_t *req, int status, addrinfo *res) { - auto backend = getStorage().get(req->data); + auto *backend = getStorage().get(req->data); if (backend) { backend->onResolved(status, res); } diff --git a/src/base/net/dns/DnsUvBackend.h b/src/base/net/dns/DnsUvBackend.h index 3c2436c7..d55fbffd 100644 --- a/src/base/net/dns/DnsUvBackend.h +++ b/src/base/net/dns/DnsUvBackend.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -16,16 +16,13 @@ * along with this program. If not, see . */ -#ifndef XMRIG_DNSUVBACKEND_H -#define XMRIG_DNSUVBACKEND_H - +#pragma once #include "base/kernel/interfaces/IDnsBackend.h" #include "base/net/dns/DnsRecords.h" #include "base/net/tools/Storage.h" - -#include +#include using uv_getaddrinfo_t = struct uv_getaddrinfo_s; @@ -45,18 +42,19 @@ public: protected: inline const DnsRecords &records() const override { return m_records; } - std::shared_ptr resolve(const String &host, IDnsListener *listener, uint64_t ttl) override; + void resolve(const String &host, const std::weak_ptr &listener, const DnsConfig &config) override; private: bool resolve(const String &host); - void done(); + void notify(); void onResolved(int status, addrinfo *res); static void onResolved(uv_getaddrinfo_t *req, int status, addrinfo *res); DnsRecords m_records; + int m_ai_family = 0; int m_status = 0; - std::queue > m_queue; + std::deque> m_queue; std::shared_ptr m_req; uint64_t m_ts = 0; uintptr_t m_key; @@ -66,7 +64,4 @@ private: }; -} /* namespace xmrig */ - - -#endif /* XMRIG_DNSUVBACKEND_H */ +} // namespace xmrig diff --git a/src/config.json b/src/config.json index 1cdedac2..48adef17 100644 --- a/src/config.json +++ b/src/config.json @@ -93,7 +93,7 @@ "dhparam": null }, "dns": { - "ipv6": false, + "ip_version": 0, "ttl": 30 }, "user-agent": null, diff --git a/src/core/config/Config_platform.h b/src/core/config/Config_platform.h index 52f66f30..c689ed8a 100644 --- a/src/core/config/Config_platform.h +++ b/src/core/config/Config_platform.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 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 @@ -16,9 +16,7 @@ * along with this program. If not, see . */ -#ifndef XMRIG_CONFIG_PLATFORM_H -#define XMRIG_CONFIG_PLATFORM_H - +#pragma once #ifdef _MSC_VER # include "getopt/getopt.h" @@ -28,13 +26,12 @@ #include "base/kernel/interfaces/IConfig.h" -#include "version.h" namespace xmrig { -static const char short_options[] = "a:c:kBp:Px:r:R:s:t:T:o:u:O:v:l:Sx:"; +static const char short_options[] = "a:c:kBp:Px:r:R:s:t:T:o:u:O:v:l:Sx:46"; static const option options[] = { @@ -99,7 +96,8 @@ static const option options[] = { { "no-title", 0, nullptr, IConfig::NoTitleKey }, { "pause-on-battery", 0, nullptr, IConfig::PauseOnBatteryKey }, { "pause-on-active", 1, nullptr, IConfig::PauseOnActiveKey }, - { "dns-ipv6", 0, nullptr, IConfig::DnsIPv6Key }, + { "ipv4", 0, nullptr, IConfig::DnsIPv4Key }, + { "ipv6", 0, nullptr, IConfig::DnsIPv6Key }, { "dns-ttl", 1, nullptr, IConfig::DnsTtlKey }, { "spend-secret-key", 1, nullptr, IConfig::SpendSecretKey }, # ifdef XMRIG_FEATURE_BENCHMARK @@ -169,6 +167,3 @@ static const option options[] = { } // namespace xmrig - - -#endif /* XMRIG_CONFIG_PLATFORM_H */ diff --git a/src/core/config/usage.h b/src/core/config/usage.h index 53a8e58c..cbec1b41 100644 --- a/src/core/config/usage.h +++ b/src/core/config/usage.h @@ -4,8 +4,8 @@ * Copyright (c) 2014 Lucas Jones * Copyright (c) 2014-2016 Wolf9466 * Copyright (c) 2016 Jay D Dee - * Copyright (c) 2018-2024 SChernykh - * Copyright (c) 2016-2024 XMRig , + * Copyright (c) 2018-2025 SChernykh + * Copyright (c) 2016-2025 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,13 +21,10 @@ * along with this program. If not, see . */ -#ifndef XMRIG_USAGE_H -#define XMRIG_USAGE_H - +#pragma once #include "version.h" - #include @@ -59,7 +56,8 @@ static inline const std::string &usage() u += " --tls-fingerprint=HEX pool TLS certificate fingerprint for strict certificate pinning\n"; # endif - u += " --dns-ipv6 prefer IPv6 records from DNS responses\n"; + u += " -4, --ipv4 resolve names to IPv4 addresses\n"; + u += " -6, --ipv6 resolve names to IPv6 addresses\n"; u += " --dns-ttl=N N seconds (default: 30) TTL for internal DNS cache\n"; # ifdef XMRIG_FEATURE_HTTP @@ -205,6 +203,4 @@ static inline const std::string &usage() } -} /* namespace xmrig */ - -#endif /* XMRIG_USAGE_H */ +} // namespace xmrig