diff --git a/src/base/net/dns/DnsRecord.cpp b/src/base/net/dns/DnsRecord.cpp index 1667c715..bfa84613 100644 --- a/src/base/net/dns/DnsRecord.cpp +++ b/src/base/net/dns/DnsRecord.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2020 SChernykh - * Copyright (c) 2016-2020 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 @@ -24,38 +24,34 @@ 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(m_data)->sin_port = htons(port); + + return reinterpret_cast(m_data); +} + + +xmrig::String xmrig::DnsRecord::ip() const { char *buf = nullptr; if (m_type == AAAA) { buf = new char[45](); - uv_ip6_name(reinterpret_cast(addr->ai_addr), buf, 45); + uv_ip6_name(reinterpret_cast(m_data), buf, 45); } else { buf = new char[16](); - uv_ip4_name(reinterpret_cast(addr->ai_addr), buf, 16); + uv_ip4_name(reinterpret_cast(m_data), buf, 16); } - m_ip = 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(addr); - } - - if (m_type == AAAA) { - auto addr = new sockaddr_in6(); - uv_ip6_addr(m_ip.data(), port, addr); - - return reinterpret_cast(addr); - } - - return nullptr; + return buf; } diff --git a/src/base/net/dns/DnsRecord.h b/src/base/net/dns/DnsRecord.h index cf6c2598..7a68ea2f 100644 --- a/src/base/net/dns/DnsRecord.h +++ b/src/base/net/dns/DnsRecord.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2020 SChernykh - * Copyright (c) 2016-2020 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 @@ -33,7 +33,7 @@ namespace xmrig { class DnsRecord { public: - enum Type { + enum Type : uint32_t { Unknown, A, AAAA @@ -42,15 +42,15 @@ public: DnsRecord() {} 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 const String &ip() const { return m_ip; } inline Type type() const { return m_type; } private: - Type m_type = Unknown; - String m_ip; + mutable uint8_t m_data[28]{}; + const Type m_type = Unknown; }; diff --git a/src/base/net/http/HttpClient.cpp b/src/base/net/http/HttpClient.cpp index ea0d68c8..09e99553 100644 --- a/src/base/net/http/HttpClient.cpp +++ b/src/base/net/http/HttpClient.cpp @@ -74,14 +74,10 @@ void xmrig::HttpClient::onResolved(const Dns &dns, int status) return; } - sockaddr *addr = dns.get().addr(port()); - auto req = new uv_connect_t; req->data = this; - uv_tcp_connect(req, m_tcp, addr, onConnect); - - delete addr; + uv_tcp_connect(req, m_tcp, dns.get().addr(port()), onConnect); } diff --git a/src/base/net/stratum/Client.cpp b/src/base/net/stratum/Client.cpp index 5327867e..7bef6055 100644 --- a/src/base/net/stratum/Client.cpp +++ b/src/base/net/stratum/Client.cpp @@ -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); @@ -584,8 +584,6 @@ void xmrig::Client::connect(sockaddr *addr) # endif uv_tcp_connect(req, m_socket, addr, onConnect); - - delete addr; } diff --git a/src/base/net/stratum/Client.h b/src/base/net/stratum/Client.h index 33e3fd8f..5721539c 100644 --- a/src/base/net/stratum/Client.h +++ b/src/base/net/stratum/Client.h @@ -108,7 +108,7 @@ private: bool write(const uv_buf_t &buf); int resolve(const String &host); int64_t send(size_t size); - void connect(sockaddr *addr); + void connect(const sockaddr *addr); void handshake(); void parse(char *line, size_t len); void parseExtensions(const rapidjson::Value &result);