New DNS implementation.

This commit is contained in:
XMRig 2021-03-16 22:24:37 +07:00
parent 5b189696d7
commit 3e41bdc552
No known key found for this signature in database
GPG key ID: 446A53638BE94409
14 changed files with 357 additions and 121 deletions

View file

@ -18,74 +18,23 @@
#include "base/net/dns/Dns.h"
#include "base/kernel/interfaces/IDnsListener.h"
#include "base/net/dns/DnsUvBackend.h"
namespace xmrig {
Storage<Dns> Dns::m_storage;
}
xmrig::Dns::Dns(IDnsListener *listener) :
m_listener(listener)
std::map<String, std::shared_ptr<IDnsBackend> > Dns::m_backends;
} // namespace xmrig
std::shared_ptr<xmrig::DnsRequest> xmrig::Dns::resolve(const String &host, IDnsListener *listener, uint64_t ttl)
{
m_key = m_storage.add(this);
m_resolver = new uv_getaddrinfo_t;
m_resolver->data = m_storage.ptr(m_key);
m_hints.ai_family = AF_UNSPEC;
m_hints.ai_socktype = SOCK_STREAM;
m_hints.ai_protocol = IPPROTO_TCP;
}
xmrig::Dns::~Dns()
{
m_storage.release(m_key);
delete m_resolver;
}
bool xmrig::Dns::resolve(const String &host)
{
if (m_host != host) {
m_host = host;
m_records.clear();
if (m_backends.find(host) == m_backends.end()) {
m_backends.insert({ host, std::make_shared<DnsUvBackend>() });
}
m_status = uv_getaddrinfo(uv_default_loop(), m_resolver, Dns::onResolved, m_host.data(), nullptr, &m_hints);
return m_status == 0;
}
void xmrig::Dns::onResolved(int status, addrinfo *res)
{
m_status = status;
if (m_status < 0) {
return m_listener->onResolved(m_records, status);
}
m_records.parse(res);
if (m_records.isEmpty()) {
m_status = UV_EAI_NONAME;
}
m_listener->onResolved(m_records, m_status);
}
void xmrig::Dns::onResolved(uv_getaddrinfo_t *req, int status, addrinfo *res)
{
Dns *dns = m_storage.get(req->data);
if (dns) {
dns->onResolved(status, res);
}
uv_freeaddrinfo(res);
return m_backends.at(host)->resolve(host, listener, ttl);
}

View file

@ -20,48 +20,28 @@
#define XMRIG_DNS_H
#include <vector>
#include <uv.h>
#include "base/tools/String.h"
#include "base/net/dns/DnsRecords.h"
#include "base/net/tools/Storage.h"
#include "base/tools/Object.h"
#include <map>
#include <memory>
namespace xmrig {
class DnsRequest;
class IDnsBackend;
class IDnsListener;
class Dns
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Dns)
Dns(IDnsListener *listener);
~Dns();
inline const String &host() const { return m_host; }
inline int status() const { return m_status; }
bool resolve(const String &host);
static std::shared_ptr<DnsRequest> resolve(const String &host, IDnsListener *listener, uint64_t ttl = 60000);
private:
void onResolved(int status, addrinfo *res);
static void onResolved(uv_getaddrinfo_t *req, int status, addrinfo *res);
addrinfo m_hints{};
DnsRecords m_records;
IDnsListener *m_listener;
int m_status = 0;
String m_host;
uintptr_t m_key;
uv_getaddrinfo_t *m_resolver = nullptr;
static Storage<Dns> m_storage;
static std::map<String, std::shared_ptr<IDnsBackend> > m_backends;
};

View file

@ -0,0 +1,50 @@
/* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 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_DNSREQUEST_H
#define XMRIG_DNSREQUEST_H
#include "base/tools/Object.h"
#include <cstdint>
namespace xmrig {
class IDnsListener;
class DnsRequest
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(DnsRequest)
DnsRequest(IDnsListener *listener) : listener(listener) {}
~DnsRequest() = default;
IDnsListener *listener;
};
} /* namespace xmrig */
#endif /* XMRIG_DNSREQUEST_H */

View file

@ -0,0 +1,130 @@
/* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 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 <uv.h>
#include "base/net/dns/DnsUvBackend.h"
#include "base/kernel/interfaces/IDnsListener.h"
#include "base/net/dns/DnsRequest.h"
#include "base/tools/Chrono.h"
namespace xmrig {
Storage<DnsUvBackend> DnsUvBackend::m_storage;
static addrinfo hints{};
} // namespace xmrig
xmrig::DnsUvBackend::DnsUvBackend()
{
if (!hints.ai_protocol) {
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
}
m_key = m_storage.add(this);
}
xmrig::DnsUvBackend::~DnsUvBackend()
{
m_storage.release(m_key);
}
std::shared_ptr<xmrig::DnsRequest> xmrig::DnsUvBackend::resolve(const String &host, IDnsListener *listener, uint64_t ttl)
{
auto req = std::make_shared<DnsRequest>(listener);
if (Chrono::currentMSecsSinceEpoch() - m_ts <= ttl && !m_records.isEmpty()) {
req->listener->onResolved(m_records, 0, nullptr);
} else {
m_queue.emplace(req);
}
if (m_queue.size() == 1 && !resolve(host)) {
done();
}
return req;
}
bool xmrig::DnsUvBackend::resolve(const String &host)
{
m_req = std::make_shared<uv_getaddrinfo_t>();
m_req->data = m_storage.ptr(m_key);
m_status = uv_getaddrinfo(uv_default_loop(), m_req.get(), DnsUvBackend::onResolved, host.data(), nullptr, &hints);
return m_status == 0;
}
void xmrig::DnsUvBackend::done()
{
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);
}
m_queue.pop();
}
m_req.reset();
}
void xmrig::DnsUvBackend::onResolved(int status, addrinfo *res)
{
m_ts = Chrono::currentMSecsSinceEpoch();
if ((m_status = status) < 0) {
return done();
}
m_records.parse(res);
if (m_records.isEmpty()) {
m_status = UV_EAI_NONAME;
}
done();
}
void xmrig::DnsUvBackend::onResolved(uv_getaddrinfo_t *req, int status, addrinfo *res)
{
auto backend = m_storage.get(req->data);
if (backend) {
backend->onResolved(status, res);
}
uv_freeaddrinfo(res);
}

View file

@ -0,0 +1,71 @@
/* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 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_DNSUVBACKEND_H
#define XMRIG_DNSUVBACKEND_H
#include "base/kernel/interfaces/IDnsBackend.h"
#include "base/net/dns/DnsRecords.h"
#include "base/net/tools/Storage.h"
#include <queue>
using uv_getaddrinfo_t = struct uv_getaddrinfo_s;
namespace xmrig {
class DnsUvBackend : public IDnsBackend
{
public:
XMRIG_DISABLE_COPY_MOVE(DnsUvBackend)
DnsUvBackend();
~DnsUvBackend() override;
protected:
inline const DnsRecords &records() const override { return m_records; }
std::shared_ptr<DnsRequest> resolve(const String &host, IDnsListener *listener, uint64_t ttl) override;
private:
bool resolve(const String &host);
void done();
void onResolved(int status, addrinfo *res);
static void onResolved(uv_getaddrinfo_t *req, int status, addrinfo *res);
DnsRecords m_records;
int m_status = 0;
std::queue<std::weak_ptr<DnsRequest> > m_queue;
std::shared_ptr<uv_getaddrinfo_t> m_req;
uint64_t m_ts = 0;
uintptr_t m_key;
static Storage<DnsUvBackend> m_storage;
};
} /* namespace xmrig */
#endif /* XMRIG_DNSUVBACKEND_H */