Improved DnsRecord class.

This commit is contained in:
XMRig 2021-03-13 20:30:52 +07:00
parent 900dd13c45
commit c6bcea3811
No known key found for this signature in database
GPG key ID: 446A53638BE94409
5 changed files with 32 additions and 42 deletions

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2021 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
@ -24,38 +24,34 @@
xmrig::DnsRecord::DnsRecord(const addrinfo *addr) : xmrig::DnsRecord::DnsRecord(const addrinfo *addr) :
m_type(addr->ai_family == AF_INET6 ? AAAA : A) 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.");
memcpy(m_data, addr->ai_addr, m_type == AAAA ? sizeof(sockaddr_in6) : sizeof(sockaddr_in));
}
const sockaddr *xmrig::DnsRecord::addr(uint16_t port) const
{
reinterpret_cast<sockaddr_in*>(m_data)->sin_port = htons(port);
return reinterpret_cast<const sockaddr *>(m_data);
}
xmrig::String xmrig::DnsRecord::ip() const
{ {
char *buf = nullptr; char *buf = nullptr;
if (m_type == AAAA) { if (m_type == AAAA) {
buf = new char[45](); buf = new char[45]();
uv_ip6_name(reinterpret_cast<sockaddr_in6*>(addr->ai_addr), buf, 45); uv_ip6_name(reinterpret_cast<sockaddr_in6*>(m_data), buf, 45);
} }
else { else {
buf = new char[16](); buf = new char[16]();
uv_ip4_name(reinterpret_cast<sockaddr_in*>(addr->ai_addr), buf, 16); uv_ip4_name(reinterpret_cast<sockaddr_in*>(m_data), buf, 16);
} }
m_ip = buf; return buf;
}
sockaddr *xmrig::DnsRecord::addr(uint16_t port) const
{
if (m_type == A) {
auto addr = new sockaddr_in();
uv_ip4_addr(m_ip.data(), port, addr);
return reinterpret_cast<sockaddr *>(addr);
}
if (m_type == AAAA) {
auto addr = new sockaddr_in6();
uv_ip6_addr(m_ip.data(), port, addr);
return reinterpret_cast<sockaddr *>(addr);
}
return nullptr;
} }

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2021 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
@ -33,7 +33,7 @@ namespace xmrig {
class DnsRecord class DnsRecord
{ {
public: public:
enum Type { enum Type : uint32_t {
Unknown, Unknown,
A, A,
AAAA AAAA
@ -42,15 +42,15 @@ public:
DnsRecord() {} DnsRecord() {}
DnsRecord(const addrinfo *addr); DnsRecord(const addrinfo *addr);
sockaddr *addr(uint16_t port = 0) const; const sockaddr *addr(uint16_t port = 0) const;
String ip() const;
inline bool isValid() const { return m_type != Unknown; } inline bool isValid() const { return m_type != Unknown; }
inline const String &ip() const { return m_ip; }
inline Type type() const { return m_type; } inline Type type() const { return m_type; }
private: private:
Type m_type = Unknown; mutable uint8_t m_data[28]{};
String m_ip; const Type m_type = Unknown;
}; };

View file

@ -74,14 +74,10 @@ void xmrig::HttpClient::onResolved(const Dns &dns, int status)
return; return;
} }
sockaddr *addr = dns.get().addr(port());
auto req = new uv_connect_t; auto req = new uv_connect_t;
req->data = this; req->data = this;
uv_tcp_connect(req, m_tcp, addr, onConnect); uv_tcp_connect(req, m_tcp, dns.get().addr(port()), onConnect);
delete addr;
} }

View file

@ -566,7 +566,7 @@ int64_t xmrig::Client::send(size_t size)
} }
void xmrig::Client::connect(sockaddr *addr) void xmrig::Client::connect(const sockaddr *addr)
{ {
setState(ConnectingState); setState(ConnectingState);
@ -584,8 +584,6 @@ void xmrig::Client::connect(sockaddr *addr)
# endif # endif
uv_tcp_connect(req, m_socket, addr, onConnect); uv_tcp_connect(req, m_socket, addr, onConnect);
delete addr;
} }

View file

@ -108,7 +108,7 @@ private:
bool write(const uv_buf_t &buf); bool write(const uv_buf_t &buf);
int resolve(const String &host); int resolve(const String &host);
int64_t send(size_t size); int64_t send(size_t size);
void connect(sockaddr *addr); void connect(const sockaddr *addr);
void handshake(); void handshake();
void parse(char *line, size_t len); void parse(char *line, size_t len);
void parseExtensions(const rapidjson::Value &result); void parseExtensions(const rapidjson::Value &result);