Merge pull request #3678 from xmrig/dns_ip_version

Improved IPv6 support.
This commit is contained in:
xmrig 2025-06-20 08:33:50 +07:00 committed by GitHub
commit 9fa66d3242
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 246 additions and 272 deletions

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -18,14 +18,12 @@
#include <cstdio> #include <cstdio>
#ifdef _MSC_VER #ifdef _MSC_VER
# include "getopt/getopt.h" # include "getopt/getopt.h"
#else #else
# include <getopt.h> # include <getopt.h>
#endif #endif
#include "base/kernel/config/BaseTransform.h" #include "base/kernel/config/BaseTransform.h"
#include "base/io/json/JsonChain.h" #include "base/io/json/JsonChain.h"
#include "base/io/log/Log.h" #include "base/io/log/Log.h"
@ -37,7 +35,6 @@
#include "base/net/stratum/Pools.h" #include "base/net/stratum/Pools.h"
#include "core/config/Config_platform.h" #include "core/config/Config_platform.h"
#ifdef XMRIG_FEATURE_TLS #ifdef XMRIG_FEATURE_TLS
# include "base/net/tls/TlsConfig.h" # include "base/net/tls/TlsConfig.h"
#endif #endif
@ -47,9 +44,9 @@ void xmrig::BaseTransform::load(JsonChain &chain, Process *process, IConfigTrans
{ {
using namespace rapidjson; using namespace rapidjson;
int key = 0; int key = 0;
int argc = process->arguments().argc(); const int argc = process->arguments().argc();
char **argv = process->arguments().argv(); char **argv = process->arguments().argv();
Document doc(kObjectType); Document doc(kObjectType);
@ -262,7 +259,8 @@ void xmrig::BaseTransform::transform(rapidjson::Document &doc, int key, const ch
case IConfig::DaemonKey: /* --daemon */ case IConfig::DaemonKey: /* --daemon */
case IConfig::SubmitToOriginKey: /* --submit-to-origin */ case IConfig::SubmitToOriginKey: /* --submit-to-origin */
case IConfig::VerboseKey: /* --verbose */ case IConfig::VerboseKey: /* --verbose */
case IConfig::DnsIPv6Key: /* --dns-ipv6 */ case IConfig::DnsIPv4Key: /* --ipv4 */
case IConfig::DnsIPv6Key: /* --ipv6 */
return transformBoolean(doc, key, true); return transformBoolean(doc, key, true);
case IConfig::ColorKey: /* --no-color */ case IConfig::ColorKey: /* --no-color */
@ -323,8 +321,11 @@ void xmrig::BaseTransform::transformBoolean(rapidjson::Document &doc, int key, b
case IConfig::NoTitleKey: /* --no-title */ case IConfig::NoTitleKey: /* --no-title */
return set(doc, BaseConfig::kTitle, enable); return set(doc, BaseConfig::kTitle, enable);
case IConfig::DnsIPv6Key: /* --dns-ipv6 */ case IConfig::DnsIPv4Key: /* --ipv4 */
return set(doc, DnsConfig::kField, DnsConfig::kIPv6, enable); return set(doc, DnsConfig::kField, DnsConfig::kIPv, 4);
case IConfig::DnsIPv6Key: /* --ipv6 */
return set(doc, DnsConfig::kField, DnsConfig::kIPv, 6);
default: default:
break; break;

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -16,9 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef XMRIG_ICONFIG_H #pragma once
#define XMRIG_ICONFIG_H
#include "3rdparty/rapidjson/fwd.h" #include "3rdparty/rapidjson/fwd.h"
@ -82,7 +80,8 @@ public:
HugePageSizeKey = 1050, HugePageSizeKey = 1050,
PauseOnActiveKey = 1051, PauseOnActiveKey = 1051,
SubmitToOriginKey = 1052, SubmitToOriginKey = 1052,
DnsIPv6Key = 1053, DnsIPv4Key = '4',
DnsIPv6Key = '6',
DnsTtlKey = 1054, DnsTtlKey = 1054,
SpendSecretKey = 1055, SpendSecretKey = 1055,
DaemonZMQPortKey = 1056, DaemonZMQPortKey = 1056,
@ -177,7 +176,4 @@ public:
}; };
} /* namespace xmrig */ } // namespace xmrig
#endif // XMRIG_ICONFIG_H

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -16,21 +16,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef XMRIG_IDNSBACKEND_H #pragma once
#define XMRIG_IDNSBACKEND_H
#include "base/tools/Object.h" #include "base/tools/Object.h"
#include <memory>
namespace xmrig { namespace xmrig {
class DnsConfig;
class DnsRecords; class DnsRecords;
class DnsRequest;
class IDnsListener; class IDnsListener;
class String; class String;
@ -43,12 +38,9 @@ public:
IDnsBackend() = default; IDnsBackend() = default;
virtual ~IDnsBackend() = default; virtual ~IDnsBackend() = default;
virtual const DnsRecords &records() const = 0; virtual const DnsRecords &records() const = 0;
virtual std::shared_ptr<DnsRequest> resolve(const String &host, IDnsListener *listener, uint64_t ttl) = 0; virtual void resolve(const String &host, const std::weak_ptr<IDnsListener> &listener, const DnsConfig &config) = 0;
}; };
} /* namespace xmrig */ } // namespace xmrig
#endif // XMRIG_IDNSBACKEND_H

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * 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/Dns.h"
#include "base/net/dns/DnsRequest.h"
#include "base/net/dns/DnsUvBackend.h" #include "base/net/dns/DnsUvBackend.h"
@ -25,17 +26,21 @@ namespace xmrig {
DnsConfig Dns::m_config; DnsConfig Dns::m_config;
std::map<String, std::shared_ptr<IDnsBackend> > Dns::m_backends; std::map<String, std::shared_ptr<IDnsBackend>> Dns::m_backends;
} // namespace xmrig } // namespace xmrig
std::shared_ptr<xmrig::DnsRequest> xmrig::Dns::resolve(const String &host, IDnsListener *listener, uint64_t ttl) std::shared_ptr<xmrig::DnsRequest> xmrig::Dns::resolve(const String &host, IDnsListener *listener)
{ {
auto req = std::make_shared<DnsRequest>(listener);
if (m_backends.find(host) == m_backends.end()) { if (m_backends.find(host) == m_backends.end()) {
m_backends.insert({ host, std::make_shared<DnsUvBackend>() }); m_backends.insert({ host, std::make_shared<DnsUvBackend>() });
} }
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;
} }

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * 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 const DnsConfig &config() { return m_config; }
inline static void set(const DnsConfig &config) { m_config = config; } inline static void set(const DnsConfig &config) { m_config = config; }
static std::shared_ptr<DnsRequest> resolve(const String &host, IDnsListener *listener, uint64_t ttl = 0); static std::shared_ptr<DnsRequest> resolve(const String &host, IDnsListener *listener);
private: private:
static DnsConfig m_config; static DnsConfig m_config;

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -20,15 +20,15 @@
#include "3rdparty/rapidjson/document.h" #include "3rdparty/rapidjson/document.h"
#include "base/io/json/Json.h" #include "base/io/json/Json.h"
#include <algorithm> #include <algorithm>
#include <uv.h>
namespace xmrig { namespace xmrig {
const char *DnsConfig::kField = "dns"; const char *DnsConfig::kField = "dns";
const char *DnsConfig::kIPv6 = "ipv6"; const char *DnsConfig::kIPv = "ip_version";
const char *DnsConfig::kTTL = "ttl"; const char *DnsConfig::kTTL = "ttl";
@ -37,8 +37,26 @@ const char *DnsConfig::kTTL = "ttl";
xmrig::DnsConfig::DnsConfig(const rapidjson::Value &value) xmrig::DnsConfig::DnsConfig(const rapidjson::Value &value)
{ {
m_ipv6 = Json::getBool(value, kIPv6, m_ipv6); const uint32_t ipv = Json::getUint(value, kIPv, m_ipv);
m_ttl = std::max(Json::getUint(value, kTTL, m_ttl), 1U); 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(); auto &allocator = doc.GetAllocator();
Value obj(kObjectType); Value obj(kObjectType);
obj.AddMember(StringRef(kIPv6), m_ipv6, allocator); obj.AddMember(StringRef(kIPv), m_ipv, allocator);
obj.AddMember(StringRef(kTTL), m_ttl, allocator); obj.AddMember(StringRef(kTTL), m_ttl, allocator);
return obj; return obj;
} }

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -16,9 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef XMRIG_DNSCONFIG_H #pragma once
#define XMRIG_DNSCONFIG_H
#include "3rdparty/rapidjson/fwd.h" #include "3rdparty/rapidjson/fwd.h"
@ -30,25 +28,22 @@ class DnsConfig
{ {
public: public:
static const char *kField; static const char *kField;
static const char *kIPv6; static const char *kIPv;
static const char *kTTL; static const char *kTTL;
DnsConfig() = default; DnsConfig() = default;
DnsConfig(const rapidjson::Value &value); 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; } inline uint32_t ttl() const { return m_ttl * 1000U; }
int ai_family() const;
rapidjson::Value toJSON(rapidjson::Document &doc) const; rapidjson::Value toJSON(rapidjson::Document &doc) const;
private: private:
bool m_ipv6 = false; uint32_t m_ttl = 30U;
uint32_t m_ttl = 30U; uint32_t m_ipv = 0U;
}; };
} /* namespace xmrig */ } // namespace xmrig
#endif /* XMRIG_DNSCONFIG_H */

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2023 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2023 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -16,19 +16,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <uv.h> #include <uv.h>
#include "base/net/dns/DnsRecord.h" #include "base/net/dns/DnsRecord.h"
xmrig::DnsRecord::DnsRecord(const addrinfo *addr) : xmrig::DnsRecord::DnsRecord(const addrinfo *addr)
m_type(addr->ai_family == AF_INET6 ? AAAA : (addr->ai_family == AF_INET ? A : Unknown))
{ {
static_assert(sizeof(m_data) >= sizeof(sockaddr_in6), "Not enough storage for IPv6 address."); 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; char *buf = nullptr;
if (m_type == AAAA) { if (reinterpret_cast<const sockaddr &>(m_data).sa_family == AF_INET6) {
buf = new char[45](); buf = new char[45]();
uv_ip6_name(reinterpret_cast<const sockaddr_in6*>(m_data), buf, 45); uv_ip6_name(reinterpret_cast<const sockaddr_in6*>(m_data), buf, 45);
} }

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -16,14 +16,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef XMRIG_DNSRECORD_H #pragma once
#define XMRIG_DNSRECORD_H
struct addrinfo; struct addrinfo;
struct sockaddr; struct sockaddr;
#include "base/tools/String.h" #include "base/tools/String.h"
@ -33,28 +30,15 @@ namespace xmrig {
class DnsRecord class DnsRecord
{ {
public: public:
enum Type : uint32_t {
Unknown,
A,
AAAA
};
DnsRecord() {} DnsRecord() {}
DnsRecord(const addrinfo *addr); DnsRecord(const addrinfo *addr);
const sockaddr *addr(uint16_t port = 0) const; const sockaddr *addr(uint16_t port = 0) const;
String ip() const; String ip() const;
inline bool isValid() const { return m_type != Unknown; }
inline Type type() const { return m_type; }
private: private:
mutable uint8_t m_data[28]{}; mutable uint8_t m_data[28]{};
const Type m_type = Unknown;
}; };
} /* namespace xmrig */ } // namespace xmrig
#endif /* XMRIG_DNSRECORD_H */

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -18,90 +18,96 @@
#include <uv.h> #include <uv.h>
#include "base/net/dns/DnsRecords.h" #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<size_t>(rand()) % size; // NOLINT(concurrency-mt-unsafe, cert-msc30-c, cert-msc50-cpp)
}
}
const xmrig::DnsRecord &xmrig::DnsRecords::get() const
{ {
static const DnsRecord defaultRecord; static const DnsRecord defaultRecord;
if (isEmpty()) { const size_t size = m_records.size();
return defaultRecord; if (size > 0) {
} return m_records[m_index++ % size];
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<size_t>(rand()) % ipv6]; // NOLINT(concurrency-mt-unsafe, cert-msc30-c, cert-msc50-cpp)
}
if (ipv4) {
return m_ipv4[ipv4 == 1 ? 0 : static_cast<size_t>(rand()) % ipv4]; // NOLINT(concurrency-mt-unsafe, cert-msc30-c, cert-msc50-cpp)
} }
return defaultRecord; 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;
}
}

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -16,9 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef XMRIG_DNSRECORDS_H #pragma once
#define XMRIG_DNSRECORDS_H
#include "base/net/dns/DnsRecord.h" #include "base/net/dns/DnsRecord.h"
@ -29,20 +27,19 @@ namespace xmrig {
class DnsRecords class DnsRecords
{ {
public: 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; inline bool isEmpty() const { return m_records.empty(); }
size_t count(DnsRecord::Type type = DnsRecord::Unknown) const; inline const std::vector<DnsRecord> &records() const { return m_records; }
void clear(); inline size_t size() const { return m_records.size(); }
void parse(addrinfo *res);
const DnsRecord &get() const;
private: private:
std::vector<DnsRecord> m_ipv4; mutable size_t m_index = 0;
std::vector<DnsRecord> m_ipv6; std::vector<DnsRecord> m_records;
}; };
} /* namespace xmrig */ } // namespace xmrig
#endif /* XMRIG_DNSRECORDS_H */

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -16,35 +16,30 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef XMRIG_DNSREQUEST_H #pragma once
#define XMRIG_DNSREQUEST_H
#include "base/kernel/interfaces/IDnsListener.h"
#include "base/tools/Object.h"
#include <cstdint>
namespace xmrig { namespace xmrig {
class IDnsListener; class DnsRequest : public IDnsListener
class DnsRequest
{ {
public: public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(DnsRequest) XMRIG_DISABLE_COPY_MOVE_DEFAULT(DnsRequest)
DnsRequest(IDnsListener *listener) : listener(listener) {} inline DnsRequest(IDnsListener *listener) : m_listener(listener) {}
~DnsRequest() = default; ~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 */ } // namespace xmrig
#endif /* XMRIG_DNSREQUEST_H */

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2023 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2023 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -16,13 +16,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <uv.h> #include <uv.h>
#include "base/net/dns/DnsUvBackend.h" #include "base/net/dns/DnsUvBackend.h"
#include "base/kernel/interfaces/IDnsListener.h" #include "base/kernel/interfaces/IDnsListener.h"
#include "base/net/dns/DnsRequest.h" #include "base/net/dns/DnsConfig.h"
#include "base/tools/Chrono.h" #include "base/tools/Chrono.h"
@ -73,21 +71,23 @@ xmrig::DnsUvBackend::~DnsUvBackend()
} }
std::shared_ptr<xmrig::DnsRequest> xmrig::DnsUvBackend::resolve(const String &host, IDnsListener *listener, uint64_t ttl) void xmrig::DnsUvBackend::resolve(const String &host, const std::weak_ptr<IDnsListener> &listener, const DnsConfig &config)
{ {
auto req = std::make_shared<DnsRequest>(listener); m_queue.emplace_back(listener);
if (Chrono::currentMSecsSinceEpoch() - m_ts <= ttl && !m_records.isEmpty()) { if (Chrono::currentMSecsSinceEpoch() - m_ts <= config.ttl()) {
req->listener->onResolved(m_records, 0, nullptr); return notify();
} else {
m_queue.emplace(req);
} }
if (m_queue.size() == 1 && !resolve(host)) { if (m_req) {
done(); 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; const char *error = m_status < 0 ? uv_strerror(m_status) : nullptr;
while (!m_queue.empty()) { for (const auto &l : m_queue) {
auto req = std::move(m_queue.front()).lock(); auto listener = l.lock();
if (req) { if (listener) {
req->listener->onResolved(m_records, m_status, error); listener->onResolved(m_records, m_status, error);
} }
m_queue.pop();
} }
m_queue.clear();
m_req.reset(); m_req.reset();
} }
void xmrig::DnsUvBackend::onResolved(int status, addrinfo *res) void xmrig::DnsUvBackend::onResolved(int status, addrinfo *res)
{ {
m_ts = Chrono::currentMSecsSinceEpoch(); m_status = status;
m_ts = Chrono::currentMSecsSinceEpoch();
if ((m_status = status) < 0) { if (m_status < 0) {
return done(); m_records = {};
return notify();
} }
m_records.parse(res); m_records = { res, m_ai_family };
if (m_records.isEmpty()) { if (m_records.isEmpty()) {
m_status = UV_EAI_NONAME; m_status = UV_EAI_NONAME;
} }
done(); notify();
} }
void xmrig::DnsUvBackend::onResolved(uv_getaddrinfo_t *req, int status, addrinfo *res) 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) { if (backend) {
backend->onResolved(status, res); backend->onResolved(status, res);
} }

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -16,16 +16,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef XMRIG_DNSUVBACKEND_H #pragma once
#define XMRIG_DNSUVBACKEND_H
#include "base/kernel/interfaces/IDnsBackend.h" #include "base/kernel/interfaces/IDnsBackend.h"
#include "base/net/dns/DnsRecords.h" #include "base/net/dns/DnsRecords.h"
#include "base/net/tools/Storage.h" #include "base/net/tools/Storage.h"
#include <deque>
#include <queue>
using uv_getaddrinfo_t = struct uv_getaddrinfo_s; using uv_getaddrinfo_t = struct uv_getaddrinfo_s;
@ -45,18 +42,19 @@ public:
protected: protected:
inline const DnsRecords &records() const override { return m_records; } inline const DnsRecords &records() const override { return m_records; }
std::shared_ptr<DnsRequest> resolve(const String &host, IDnsListener *listener, uint64_t ttl) override; void resolve(const String &host, const std::weak_ptr<IDnsListener> &listener, const DnsConfig &config) override;
private: private:
bool resolve(const String &host); bool resolve(const String &host);
void done(); void notify();
void onResolved(int status, addrinfo *res); void onResolved(int status, addrinfo *res);
static void onResolved(uv_getaddrinfo_t *req, int status, addrinfo *res); static void onResolved(uv_getaddrinfo_t *req, int status, addrinfo *res);
DnsRecords m_records; DnsRecords m_records;
int m_ai_family = 0;
int m_status = 0; int m_status = 0;
std::queue<std::weak_ptr<DnsRequest> > m_queue; std::deque<std::weak_ptr<IDnsListener>> m_queue;
std::shared_ptr<uv_getaddrinfo_t> m_req; std::shared_ptr<uv_getaddrinfo_t> m_req;
uint64_t m_ts = 0; uint64_t m_ts = 0;
uintptr_t m_key; uintptr_t m_key;
@ -66,7 +64,4 @@ private:
}; };
} /* namespace xmrig */ } // namespace xmrig
#endif /* XMRIG_DNSUVBACKEND_H */

View file

@ -93,7 +93,7 @@
"dhparam": null "dhparam": null
}, },
"dns": { "dns": {
"ipv6": false, "ip_version": 0,
"ttl": 30 "ttl": 30
}, },
"user-agent": null, "user-agent": null,

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -16,9 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef XMRIG_CONFIG_PLATFORM_H #pragma once
#define XMRIG_CONFIG_PLATFORM_H
#ifdef _MSC_VER #ifdef _MSC_VER
# include "getopt/getopt.h" # include "getopt/getopt.h"
@ -28,13 +26,12 @@
#include "base/kernel/interfaces/IConfig.h" #include "base/kernel/interfaces/IConfig.h"
#include "version.h"
namespace xmrig { 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[] = { static const option options[] = {
@ -99,7 +96,8 @@ static const option options[] = {
{ "no-title", 0, nullptr, IConfig::NoTitleKey }, { "no-title", 0, nullptr, IConfig::NoTitleKey },
{ "pause-on-battery", 0, nullptr, IConfig::PauseOnBatteryKey }, { "pause-on-battery", 0, nullptr, IConfig::PauseOnBatteryKey },
{ "pause-on-active", 1, nullptr, IConfig::PauseOnActiveKey }, { "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 }, { "dns-ttl", 1, nullptr, IConfig::DnsTtlKey },
{ "spend-secret-key", 1, nullptr, IConfig::SpendSecretKey }, { "spend-secret-key", 1, nullptr, IConfig::SpendSecretKey },
# ifdef XMRIG_FEATURE_BENCHMARK # ifdef XMRIG_FEATURE_BENCHMARK
@ -169,6 +167,3 @@ static const option options[] = {
} // namespace xmrig } // namespace xmrig
#endif /* XMRIG_CONFIG_PLATFORM_H */

View file

@ -4,8 +4,8 @@
* Copyright (c) 2014 Lucas Jones <https://github.com/lucasjones> * Copyright (c) 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright (c) 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright (c) 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright (c) 2016 Jay D Dee <jayddee246@gmail.com> * Copyright (c) 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright (c) 2018-2024 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2024 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -21,13 +21,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef XMRIG_USAGE_H #pragma once
#define XMRIG_USAGE_H
#include "version.h" #include "version.h"
#include <string> #include <string>
@ -59,7 +56,8 @@ static inline const std::string &usage()
u += " --tls-fingerprint=HEX pool TLS certificate fingerprint for strict certificate pinning\n"; u += " --tls-fingerprint=HEX pool TLS certificate fingerprint for strict certificate pinning\n";
# endif # 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"; u += " --dns-ttl=N N seconds (default: 30) TTL for internal DNS cache\n";
# ifdef XMRIG_FEATURE_HTTP # ifdef XMRIG_FEATURE_HTTP
@ -205,6 +203,4 @@ static inline const std::string &usage()
} }
} /* namespace xmrig */ } // namespace xmrig
#endif /* XMRIG_USAGE_H */