2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2018-11-27 19:26:32 +01:00
|
|
|
#define _WINSOCK_DEPRECATED_NO_WARNINGS // we need to use inet_addr instead of inet_pton
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/IPAddress.h"
|
|
|
|
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/port/SocketFd.h"
|
|
|
|
#include "td/utils/port/thread_local.h"
|
|
|
|
#include "td/utils/ScopeGuard.h"
|
2018-11-15 18:10:20 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-05-18 15:13:35 +02:00
|
|
|
#include "td/utils/utf8.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-05-18 15:13:35 +02:00
|
|
|
#if TD_WINDOWS
|
|
|
|
#include "td/utils/port/wstring_convert.h"
|
|
|
|
#else
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <cstring>
|
2018-11-15 18:10:20 +01:00
|
|
|
#include <limits>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2018-05-18 15:13:35 +02:00
|
|
|
static bool is_ascii_host_char(char c) {
|
2018-05-18 20:12:39 +02:00
|
|
|
return static_cast<unsigned char>(c) <= 127;
|
2018-05-18 15:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_ascii_host(Slice host) {
|
|
|
|
for (auto c : host) {
|
2018-05-18 20:12:39 +02:00
|
|
|
if (!is_ascii_host_char(c)) {
|
2018-05-18 15:13:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-18 22:38:11 +02:00
|
|
|
#if !TD_WINDOWS
|
|
|
|
static void punycode(string &result, Slice part) {
|
|
|
|
vector<uint32> codes;
|
|
|
|
codes.reserve(utf8_length(part));
|
|
|
|
uint32 processed = 0;
|
|
|
|
auto begin = part.ubegin();
|
|
|
|
auto end = part.uend();
|
|
|
|
while (begin != end) {
|
|
|
|
uint32 code;
|
2022-08-19 15:37:44 +02:00
|
|
|
begin = next_utf8_unsafe(begin, &code);
|
2018-05-18 22:38:11 +02:00
|
|
|
if (code <= 127u) {
|
|
|
|
result += to_lower(static_cast<char>(code));
|
|
|
|
processed++;
|
|
|
|
}
|
|
|
|
codes.push_back(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (processed > 0) {
|
|
|
|
result += '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 n = 127;
|
|
|
|
uint32 delta = 0;
|
|
|
|
int bias = -72;
|
|
|
|
bool is_first = true;
|
|
|
|
while (processed < codes.size()) {
|
|
|
|
// choose lowest not processed code
|
|
|
|
uint32 next_n = 0x110000;
|
|
|
|
for (auto code : codes) {
|
|
|
|
if (code > n && code < next_n) {
|
|
|
|
next_n = code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delta += (next_n - n - 1) * (processed + 1);
|
|
|
|
|
|
|
|
for (auto code : codes) {
|
|
|
|
if (code < next_n) {
|
|
|
|
delta++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code == next_n) {
|
|
|
|
// found next symbol, encode delta
|
2021-10-20 00:27:02 +02:00
|
|
|
auto left = static_cast<int>(delta);
|
2018-05-18 22:38:11 +02:00
|
|
|
while (true) {
|
|
|
|
bias += 36;
|
|
|
|
auto t = clamp(bias, 1, 26);
|
|
|
|
if (left < t) {
|
|
|
|
result += static_cast<char>(left + 'a');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
left -= t;
|
|
|
|
auto digit = t + left % (36 - t);
|
|
|
|
result += static_cast<char>(digit < 26 ? digit + 'a' : digit - 26 + '0');
|
|
|
|
left /= 36 - t;
|
|
|
|
}
|
|
|
|
processed++;
|
|
|
|
|
|
|
|
// update bias
|
|
|
|
if (is_first) {
|
|
|
|
delta /= 700;
|
|
|
|
is_first = false;
|
|
|
|
} else {
|
|
|
|
delta /= 2;
|
|
|
|
}
|
|
|
|
delta += delta / processed;
|
|
|
|
|
|
|
|
bias = 0;
|
|
|
|
while (delta > 35 * 13) {
|
|
|
|
delta /= 35;
|
|
|
|
bias -= 36;
|
|
|
|
}
|
|
|
|
bias -= static_cast<int>(36 * delta / (delta + 38));
|
|
|
|
delta = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delta++;
|
|
|
|
n = next_n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-05-18 15:13:35 +02:00
|
|
|
Result<string> idn_to_ascii(CSlice host) {
|
|
|
|
if (is_ascii_host(host)) {
|
|
|
|
return to_lower(host);
|
|
|
|
}
|
|
|
|
if (!check_utf8(host)) {
|
|
|
|
return Status::Error("Host name must be encoded in UTF-8");
|
|
|
|
}
|
|
|
|
|
2018-05-18 22:38:11 +02:00
|
|
|
const int MAX_DNS_NAME_LENGTH = 255;
|
|
|
|
if (host.size() >= MAX_DNS_NAME_LENGTH * 4) { // upper bound, 4 characters per symbol
|
|
|
|
return Status::Error("Host name is too long");
|
|
|
|
}
|
|
|
|
|
2018-05-18 15:13:35 +02:00
|
|
|
#if TD_WINDOWS
|
|
|
|
TRY_RESULT(whost, to_wstring(host));
|
2018-05-18 22:38:11 +02:00
|
|
|
wchar_t punycode[MAX_DNS_NAME_LENGTH + 1];
|
2018-06-28 15:50:43 +02:00
|
|
|
int result_length =
|
|
|
|
IdnToAscii(IDN_ALLOW_UNASSIGNED, whost.c_str(), narrow_cast<int>(whost.size()), punycode, MAX_DNS_NAME_LENGTH);
|
2018-05-18 15:13:35 +02:00
|
|
|
if (result_length == 0) {
|
2018-05-18 20:12:39 +02:00
|
|
|
return Status::Error("Host can't be converted to ASCII");
|
2018-05-18 15:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TRY_RESULT(idn_host, from_wstring(punycode, result_length));
|
|
|
|
return idn_host;
|
|
|
|
#else
|
2018-05-18 22:38:11 +02:00
|
|
|
auto parts = full_split(Slice(host), '.');
|
|
|
|
bool is_first = true;
|
|
|
|
string result;
|
2019-02-04 03:39:20 +01:00
|
|
|
result.reserve(host.size());
|
2018-05-18 22:38:11 +02:00
|
|
|
for (auto part : parts) {
|
|
|
|
if (!is_first) {
|
|
|
|
result += '.';
|
|
|
|
}
|
|
|
|
if (is_ascii_host(part)) {
|
|
|
|
result.append(part.data(), part.size());
|
|
|
|
} else {
|
|
|
|
// TODO nameprep should be applied first, but punycode is better than nothing.
|
|
|
|
// It is better to use libidn/ICU here if available
|
|
|
|
result += "xn--";
|
|
|
|
punycode(result, part);
|
|
|
|
}
|
|
|
|
is_first = false;
|
|
|
|
}
|
|
|
|
return result;
|
2018-05-18 15:13:35 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-27 19:26:32 +01:00
|
|
|
static CSlice get_ip_str(int family, const void *addr) {
|
|
|
|
const int buf_size = INET6_ADDRSTRLEN;
|
|
|
|
static TD_THREAD_LOCAL char *buf;
|
|
|
|
init_thread_local<char[]>(buf, buf_size);
|
|
|
|
|
|
|
|
const char *res = inet_ntop(family,
|
|
|
|
#if TD_WINDOWS
|
|
|
|
const_cast<PVOID>(addr),
|
|
|
|
#else
|
|
|
|
addr,
|
|
|
|
#endif
|
|
|
|
buf, buf_size);
|
|
|
|
if (res == nullptr) {
|
|
|
|
return CSlice();
|
|
|
|
} else {
|
|
|
|
return CSlice(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
IPAddress::IPAddress() : is_valid_(false) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IPAddress::is_valid() const {
|
|
|
|
return is_valid_;
|
|
|
|
}
|
|
|
|
|
2018-11-15 18:10:20 +01:00
|
|
|
bool IPAddress::is_reserved() const {
|
|
|
|
CHECK(is_valid());
|
|
|
|
|
|
|
|
if (is_ipv6()) {
|
|
|
|
// TODO proper check for reserved IPv6 addresses
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 ip = get_ipv4();
|
|
|
|
struct IpBlock {
|
|
|
|
CSlice ip;
|
|
|
|
int mask;
|
|
|
|
IpBlock(CSlice ip, int mask) : ip(ip), mask(mask) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static const IpBlock blocks[] = {{"0.0.0.0", 8}, {"10.0.0.0", 8}, {"100.64.0.0", 10}, {"127.0.0.0", 8},
|
|
|
|
{"169.254.0.0", 16}, {"172.16.0.0", 12}, {"192.0.0.0", 24}, {"192.0.2.0", 24},
|
|
|
|
{"192.88.99.0", 24}, {"192.168.0.0", 16}, {"198.18.0.0", 15}, {"198.51.100.0", 24},
|
|
|
|
{"203.0.113.0", 24}, {"224.0.0.0", 3}};
|
|
|
|
for (auto &block : blocks) {
|
|
|
|
IPAddress block_ip_address;
|
|
|
|
block_ip_address.init_ipv4_port(block.ip, 80).ensure();
|
|
|
|
uint32 range = block_ip_address.get_ipv4();
|
|
|
|
CHECK(block.mask != 0);
|
|
|
|
uint32 mask = std::numeric_limits<uint32>::max() >> (32 - block.mask) << (32 - block.mask);
|
|
|
|
if ((ip & mask) == (range & mask)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
const sockaddr *IPAddress::get_sockaddr() const {
|
2018-06-29 19:36:27 +02:00
|
|
|
CHECK(is_valid());
|
2018-12-31 20:04:05 +01:00
|
|
|
return &sockaddr_;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t IPAddress::get_sockaddr_len() const {
|
|
|
|
CHECK(is_valid());
|
2018-08-13 19:15:09 +02:00
|
|
|
switch (sockaddr_.sa_family) {
|
2018-12-31 20:04:05 +01:00
|
|
|
case AF_INET6:
|
|
|
|
return sizeof(ipv6_addr_);
|
|
|
|
case AF_INET:
|
|
|
|
return sizeof(ipv4_addr_);
|
|
|
|
default:
|
2020-07-13 20:49:07 +02:00
|
|
|
UNREACHABLE();
|
2018-02-11 15:52:41 +01:00
|
|
|
return 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int IPAddress::get_address_family() const {
|
|
|
|
return get_sockaddr()->sa_family;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IPAddress::is_ipv4() const {
|
2018-06-29 19:36:27 +02:00
|
|
|
return is_valid() && get_address_family() == AF_INET;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IPAddress::is_ipv6() const {
|
|
|
|
return is_valid() && get_address_family() == AF_INET6;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 IPAddress::get_ipv4() const {
|
|
|
|
CHECK(is_valid());
|
|
|
|
CHECK(is_ipv4());
|
2018-09-11 18:40:53 +02:00
|
|
|
return htonl(ipv4_addr_.sin_addr.s_addr);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-05-16 15:11:03 +02:00
|
|
|
string IPAddress::get_ipv6() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
static_assert(sizeof(ipv6_addr_.sin6_addr) == 16, "ipv6 size == 16");
|
|
|
|
CHECK(is_valid());
|
|
|
|
CHECK(!is_ipv4());
|
2020-05-16 15:11:03 +02:00
|
|
|
return Slice(ipv6_addr_.sin6_addr.s6_addr, 16).str();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress IPAddress::get_any_addr() const {
|
|
|
|
IPAddress res;
|
|
|
|
switch (get_address_family()) {
|
|
|
|
case AF_INET6:
|
|
|
|
res.init_ipv6_any();
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
res.init_ipv4_any();
|
|
|
|
break;
|
|
|
|
default:
|
2020-07-13 20:49:07 +02:00
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2018-05-18 15:13:35 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void IPAddress::init_ipv4_any() {
|
|
|
|
is_valid_ = true;
|
2018-12-17 20:09:01 +01:00
|
|
|
std::memset(&ipv4_addr_, 0, sizeof(ipv4_addr_));
|
2018-12-31 20:04:05 +01:00
|
|
|
ipv4_addr_.sin_family = AF_INET;
|
|
|
|
ipv4_addr_.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
ipv4_addr_.sin_port = 0;
|
|
|
|
}
|
2018-05-18 15:13:35 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void IPAddress::init_ipv6_any() {
|
|
|
|
is_valid_ = true;
|
2018-12-17 20:09:01 +01:00
|
|
|
std::memset(&ipv6_addr_, 0, sizeof(ipv6_addr_));
|
2018-12-31 20:04:05 +01:00
|
|
|
ipv6_addr_.sin6_family = AF_INET6;
|
|
|
|
ipv6_addr_.sin6_addr = in6addr_any;
|
|
|
|
ipv6_addr_.sin6_port = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status IPAddress::init_ipv6_port(CSlice ipv6, int port) {
|
|
|
|
is_valid_ = false;
|
|
|
|
if (port <= 0 || port >= (1 << 16)) {
|
2020-05-16 22:54:40 +02:00
|
|
|
return Status::Error(PSLICE() << "Invalid [IPv6 address port=" << port << "]");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-05-16 16:30:16 +02:00
|
|
|
string ipv6_plain;
|
|
|
|
if (ipv6.size() > 2 && ipv6[0] == '[' && ipv6.back() == ']') {
|
|
|
|
ipv6_plain.assign(ipv6.begin() + 1, ipv6.size() - 2);
|
|
|
|
ipv6 = ipv6_plain;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
std::memset(&ipv6_addr_, 0, sizeof(ipv6_addr_));
|
|
|
|
ipv6_addr_.sin6_family = AF_INET6;
|
|
|
|
ipv6_addr_.sin6_port = htons(static_cast<uint16>(port));
|
|
|
|
int err = inet_pton(AF_INET6, ipv6.c_str(), &ipv6_addr_.sin6_addr);
|
|
|
|
if (err == 0) {
|
|
|
|
return Status::Error(PSLICE() << "Failed inet_pton(AF_INET6, " << ipv6 << ")");
|
|
|
|
} else if (err == -1) {
|
|
|
|
return OS_SOCKET_ERROR(PSLICE() << "Failed inet_pton(AF_INET6, " << ipv6 << ")");
|
|
|
|
}
|
|
|
|
is_valid_ = true;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status IPAddress::init_ipv6_as_ipv4_port(CSlice ipv4, int port) {
|
|
|
|
return init_ipv6_port(string("::FFFF:").append(ipv4.begin(), ipv4.size()), port);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status IPAddress::init_ipv4_port(CSlice ipv4, int port) {
|
|
|
|
is_valid_ = false;
|
|
|
|
if (port <= 0 || port >= (1 << 16)) {
|
2020-05-16 22:54:40 +02:00
|
|
|
return Status::Error(PSLICE() << "Invalid [IPv4 address port=" << port << "]");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
std::memset(&ipv4_addr_, 0, sizeof(ipv4_addr_));
|
|
|
|
ipv4_addr_.sin_family = AF_INET;
|
|
|
|
ipv4_addr_.sin_port = htons(static_cast<uint16>(port));
|
|
|
|
int err = inet_pton(AF_INET, ipv4.c_str(), &ipv4_addr_.sin_addr);
|
|
|
|
if (err == 0) {
|
|
|
|
return Status::Error(PSLICE() << "Failed inet_pton(AF_INET, " << ipv4 << ")");
|
|
|
|
} else if (err == -1) {
|
|
|
|
return OS_SOCKET_ERROR(PSLICE() << "Failed inet_pton(AF_INET, " << ipv4 << ")");
|
|
|
|
}
|
|
|
|
is_valid_ = true;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2020-05-16 16:35:53 +02:00
|
|
|
Result<IPAddress> IPAddress::get_ip_address(CSlice host) {
|
|
|
|
auto r_address = get_ipv4_address(host);
|
|
|
|
if (r_address.is_ok()) {
|
|
|
|
return r_address.move_as_ok();
|
|
|
|
}
|
|
|
|
r_address = get_ipv6_address(host);
|
|
|
|
if (r_address.is_ok()) {
|
|
|
|
return r_address.move_as_ok();
|
|
|
|
}
|
2020-11-09 22:31:56 +01:00
|
|
|
return Status::Error(PSLICE() << '"' << host << "\" is not a valid IP address");
|
2020-05-16 16:35:53 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 04:32:10 +01:00
|
|
|
Result<IPAddress> IPAddress::get_ipv4_address(CSlice host) {
|
|
|
|
// sometimes inet_addr allows much more valid IPv4 hosts than inet_pton,
|
|
|
|
// like 0x12.0x34.0x56.0x78, or 0x12345678, or 0x7f.001
|
|
|
|
auto ipv4_numeric_addr = inet_addr(host.c_str());
|
|
|
|
if (ipv4_numeric_addr == INADDR_NONE) {
|
2020-11-09 22:31:56 +01:00
|
|
|
return Status::Error(PSLICE() << '"' << host << "\" is not a valid IPv4 address");
|
2019-02-04 04:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
host = ::td::get_ip_str(AF_INET, &ipv4_numeric_addr);
|
|
|
|
IPAddress result;
|
|
|
|
auto status = result.init_ipv4_port(host, 1);
|
|
|
|
if (status.is_error()) {
|
|
|
|
return std::move(status);
|
|
|
|
}
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<IPAddress> IPAddress::get_ipv6_address(CSlice host) {
|
|
|
|
IPAddress result;
|
|
|
|
auto status = result.init_ipv6_port(host, 1);
|
|
|
|
if (status.is_error()) {
|
2020-11-09 22:31:56 +01:00
|
|
|
return Status::Error(PSLICE() << '"' << host << "\" is not a valid IPv6 address");
|
2019-02-04 04:32:10 +01:00
|
|
|
}
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
|
2018-07-01 03:12:20 +02:00
|
|
|
Status IPAddress::init_host_port(CSlice host, int port, bool prefer_ipv6) {
|
2020-05-16 21:53:19 +02:00
|
|
|
if (host.size() > 2 && host[0] == '[' && host.back() == ']') {
|
2020-05-16 22:54:40 +02:00
|
|
|
return init_ipv6_port(host, port == 0 ? 1 : port);
|
2020-05-16 21:53:19 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 00:29:37 +01:00
|
|
|
return init_host_port(host, PSLICE() << port, prefer_ipv6);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-07-01 03:12:20 +02:00
|
|
|
Status IPAddress::init_host_port(CSlice host, CSlice port, bool prefer_ipv6) {
|
2018-11-27 19:26:32 +01:00
|
|
|
is_valid_ = false;
|
2018-05-15 19:07:03 +02:00
|
|
|
if (host.empty()) {
|
|
|
|
return Status::Error("Host is empty");
|
|
|
|
}
|
|
|
|
#if TD_WINDOWS
|
|
|
|
if (host == "..localmachine") {
|
|
|
|
return Status::Error("Host is invalid");
|
|
|
|
}
|
|
|
|
#endif
|
2018-05-18 15:13:35 +02:00
|
|
|
TRY_RESULT(ascii_host, idn_to_ascii(host));
|
2019-12-08 07:57:33 +01:00
|
|
|
host = ascii_host; // assign string to CSlice
|
2018-05-18 15:13:35 +02:00
|
|
|
|
2020-05-16 21:53:19 +02:00
|
|
|
if (host[0] == '[' && host.back() == ']') {
|
2020-05-16 22:54:40 +02:00
|
|
|
auto port_int = to_integer<int>(port);
|
|
|
|
return init_ipv6_port(host, port_int == 0 ? 1 : port_int);
|
2020-05-16 21:53:19 +02:00
|
|
|
}
|
|
|
|
|
2018-11-27 19:26:32 +01:00
|
|
|
// some getaddrinfo implementations use inet_pton instead of inet_aton and support only decimal-dotted IPv4 form,
|
|
|
|
// and so doesn't recognize 0x12.0x34.0x56.0x78, or 0x12345678, or 0x7f.001 as valid IPv4 addresses
|
|
|
|
auto ipv4_numeric_addr = inet_addr(host.c_str());
|
|
|
|
if (ipv4_numeric_addr != INADDR_NONE) {
|
|
|
|
host = ::td::get_ip_str(AF_INET, &ipv4_numeric_addr);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
addrinfo hints;
|
|
|
|
addrinfo *info = nullptr;
|
|
|
|
std::memset(&hints, 0, sizeof(hints));
|
2018-07-01 01:10:17 +02:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
2018-12-31 20:04:05 +01:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2018-07-01 01:10:17 +02:00
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
2020-06-18 04:02:18 +02:00
|
|
|
LOG(DEBUG + 10) << "Trying to init IP address of " << host << " with port " << port;
|
2018-05-18 15:13:35 +02:00
|
|
|
auto err = getaddrinfo(host.c_str(), port.c_str(), &hints, &info);
|
|
|
|
if (err != 0) {
|
2018-05-17 20:21:19 +02:00
|
|
|
#if TD_WINDOWS
|
|
|
|
return OS_SOCKET_ERROR("Failed to resolve host");
|
|
|
|
#else
|
2018-05-18 15:13:35 +02:00
|
|
|
return Status::Error(PSLICE() << "Failed to resolve host: " << gai_strerror(err));
|
2018-05-17 20:21:19 +02:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
SCOPE_EXIT {
|
|
|
|
freeaddrinfo(info);
|
|
|
|
};
|
|
|
|
|
2018-07-01 01:10:17 +02:00
|
|
|
addrinfo *best_info = nullptr;
|
|
|
|
for (auto *ptr = info; ptr != nullptr; ptr = ptr->ai_next) {
|
2018-07-01 03:12:20 +02:00
|
|
|
if (ptr->ai_family == AF_INET && (!prefer_ipv6 || best_info == nullptr)) {
|
|
|
|
// just use first IPv4 address if there is no IPv6 and it isn't preferred
|
2018-12-31 20:04:05 +01:00
|
|
|
best_info = ptr;
|
2018-07-01 03:12:20 +02:00
|
|
|
if (!prefer_ipv6) {
|
|
|
|
break;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-07-01 03:12:20 +02:00
|
|
|
if (ptr->ai_family == AF_INET6 && (prefer_ipv6 || best_info == nullptr)) {
|
|
|
|
// or first IPv6 address if there is no IPv4 and it isn't preferred
|
2018-07-01 01:10:17 +02:00
|
|
|
best_info = ptr;
|
2018-07-01 03:12:20 +02:00
|
|
|
if (prefer_ipv6) {
|
|
|
|
break;
|
|
|
|
}
|
2018-07-01 01:10:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (best_info == nullptr) {
|
|
|
|
return Status::Error("Failed to find IPv4/IPv6 address");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return init_sockaddr(best_info->ai_addr, narrow_cast<socklen_t>(best_info->ai_addrlen));
|
|
|
|
}
|
|
|
|
|
|
|
|
Status IPAddress::init_host_port(CSlice host_port) {
|
|
|
|
auto pos = host_port.rfind(':');
|
|
|
|
if (pos == static_cast<size_t>(-1)) {
|
|
|
|
return Status::Error("Can't split string into host and port");
|
|
|
|
}
|
|
|
|
return init_host_port(host_port.substr(0, pos).str(), host_port.substr(pos + 1).str());
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
Status IPAddress::init_sockaddr(sockaddr *addr) {
|
|
|
|
if (addr->sa_family == AF_INET6) {
|
|
|
|
return init_sockaddr(addr, sizeof(ipv6_addr_));
|
|
|
|
} else if (addr->sa_family == AF_INET) {
|
|
|
|
return init_sockaddr(addr, sizeof(ipv4_addr_));
|
|
|
|
} else {
|
|
|
|
return init_sockaddr(addr, 0);
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
Status IPAddress::init_sockaddr(sockaddr *addr, socklen_t len) {
|
|
|
|
if (addr->sa_family == AF_INET6) {
|
|
|
|
CHECK(len == sizeof(ipv6_addr_));
|
|
|
|
std::memcpy(&ipv6_addr_, reinterpret_cast<sockaddr_in6 *>(addr), sizeof(ipv6_addr_));
|
|
|
|
} else if (addr->sa_family == AF_INET) {
|
|
|
|
CHECK(len == sizeof(ipv4_addr_));
|
|
|
|
std::memcpy(&ipv4_addr_, reinterpret_cast<sockaddr_in *>(addr), sizeof(ipv4_addr_));
|
|
|
|
} else {
|
|
|
|
return Status::Error(PSLICE() << "Unknown " << tag("sa_family", addr->sa_family));
|
|
|
|
}
|
|
|
|
|
|
|
|
is_valid_ = true;
|
2019-07-06 13:29:15 +02:00
|
|
|
LOG(DEBUG + 10) << "Have address " << get_ip_str() << " with port " << get_port();
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status IPAddress::init_socket_address(const SocketFd &socket_fd) {
|
|
|
|
is_valid_ = false;
|
2021-03-26 16:04:01 +01:00
|
|
|
if (socket_fd.empty()) {
|
|
|
|
return Status::Error("Socket is empty");
|
|
|
|
}
|
2018-09-11 15:27:20 +02:00
|
|
|
auto socket = socket_fd.get_native_fd().socket();
|
2018-08-13 19:15:09 +02:00
|
|
|
socklen_t len = storage_size();
|
2018-09-11 15:27:20 +02:00
|
|
|
int ret = getsockname(socket, &sockaddr_, &len);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return OS_SOCKET_ERROR("Failed to get socket address");
|
|
|
|
}
|
|
|
|
is_valid_ = true;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status IPAddress::init_peer_address(const SocketFd &socket_fd) {
|
|
|
|
is_valid_ = false;
|
2021-03-26 16:04:01 +01:00
|
|
|
if (socket_fd.empty()) {
|
|
|
|
return Status::Error("Socket is empty");
|
|
|
|
}
|
2018-09-11 15:27:20 +02:00
|
|
|
auto socket = socket_fd.get_native_fd().socket();
|
2018-08-13 19:15:09 +02:00
|
|
|
socklen_t len = storage_size();
|
2018-09-11 15:27:20 +02:00
|
|
|
int ret = getpeername(socket, &sockaddr_, &len);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return OS_SOCKET_ERROR("Failed to get peer socket address");
|
|
|
|
}
|
|
|
|
is_valid_ = true;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:22:40 +02:00
|
|
|
void IPAddress::clear_ipv6_interface() {
|
|
|
|
if (!is_valid() || get_address_family() != AF_INET6) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *begin = ipv6_addr_.sin6_addr.s6_addr;
|
|
|
|
static_assert(sizeof(ipv6_addr_.sin6_addr.s6_addr) == 16, "expected 16 bytes buffer for ipv6");
|
|
|
|
static_assert(sizeof(*begin) == 1, "expected array of bytes");
|
2020-07-14 19:54:38 +02:00
|
|
|
std::memset(begin + 8, 0, 8 * sizeof(*begin));
|
2020-07-14 16:22:40 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 15:33:32 +02:00
|
|
|
string IPAddress::ipv4_to_str(uint32 ipv4) {
|
2018-09-11 20:49:39 +02:00
|
|
|
ipv4 = ntohl(ipv4);
|
2020-05-16 15:33:32 +02:00
|
|
|
return ::td::get_ip_str(AF_INET, &ipv4).str();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-05-16 15:33:32 +02:00
|
|
|
string IPAddress::ipv6_to_str(Slice ipv6) {
|
2019-07-06 13:29:15 +02:00
|
|
|
CHECK(ipv6.size() == 16);
|
2020-05-16 15:33:32 +02:00
|
|
|
return ::td::get_ip_str(AF_INET6, ipv6.ubegin()).str();
|
2019-07-06 13:29:15 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 17:43:49 +02:00
|
|
|
CSlice IPAddress::get_ip_str() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!is_valid()) {
|
2020-05-16 17:43:49 +02:00
|
|
|
return CSlice("0.0.0.0");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (get_address_family()) {
|
|
|
|
case AF_INET6:
|
2018-04-30 15:30:50 +02:00
|
|
|
return ::td::get_ip_str(AF_INET6, &ipv6_addr_.sin6_addr);
|
2018-12-31 20:04:05 +01:00
|
|
|
case AF_INET:
|
2018-04-30 15:30:50 +02:00
|
|
|
return ::td::get_ip_str(AF_INET, &ipv4_addr_.sin_addr);
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2020-05-16 17:43:49 +02:00
|
|
|
return CSlice();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 22:12:52 +02:00
|
|
|
string IPAddress::get_ip_host() const {
|
|
|
|
if (!is_valid()) {
|
|
|
|
return "0.0.0.0";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (get_address_family()) {
|
|
|
|
case AF_INET6:
|
|
|
|
return PSTRING() << '[' << ::td::get_ip_str(AF_INET6, &ipv6_addr_.sin6_addr) << ']';
|
|
|
|
case AF_INET:
|
|
|
|
return ::td::get_ip_str(AF_INET, &ipv4_addr_.sin_addr).str();
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
int IPAddress::get_port() const {
|
|
|
|
if (!is_valid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (get_address_family()) {
|
|
|
|
case AF_INET6:
|
|
|
|
return ntohs(ipv6_addr_.sin6_port);
|
|
|
|
case AF_INET:
|
|
|
|
return ntohs(ipv4_addr_.sin_port);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPAddress::set_port(int port) {
|
|
|
|
CHECK(is_valid());
|
|
|
|
|
|
|
|
switch (get_address_family()) {
|
|
|
|
case AF_INET6:
|
|
|
|
ipv6_addr_.sin6_port = htons(static_cast<uint16>(port));
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
ipv4_addr_.sin_port = htons(static_cast<uint16>(port));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const IPAddress &a, const IPAddress &b) {
|
|
|
|
if (!a.is_valid() || !b.is_valid()) {
|
2018-06-29 19:36:27 +02:00
|
|
|
return !a.is_valid() && !b.is_valid();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (a.get_address_family() != b.get_address_family()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.get_address_family() == AF_INET) {
|
|
|
|
return a.ipv4_addr_.sin_port == b.ipv4_addr_.sin_port &&
|
|
|
|
std::memcmp(&a.ipv4_addr_.sin_addr, &b.ipv4_addr_.sin_addr, sizeof(a.ipv4_addr_.sin_addr)) == 0;
|
|
|
|
} else if (a.get_address_family() == AF_INET6) {
|
|
|
|
return a.ipv6_addr_.sin6_port == b.ipv6_addr_.sin6_port &&
|
|
|
|
std::memcmp(&a.ipv6_addr_.sin6_addr, &b.ipv6_addr_.sin6_addr, sizeof(a.ipv6_addr_.sin6_addr)) == 0;
|
|
|
|
}
|
|
|
|
|
2020-07-13 20:49:07 +02:00
|
|
|
UNREACHABLE();
|
2018-12-31 20:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const IPAddress &a, const IPAddress &b) {
|
2018-06-29 19:36:27 +02:00
|
|
|
if (!a.is_valid() || !b.is_valid()) {
|
|
|
|
return !a.is_valid() && b.is_valid();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (a.get_address_family() != b.get_address_family()) {
|
|
|
|
return a.get_address_family() < b.get_address_family();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.get_address_family() == AF_INET) {
|
|
|
|
if (a.ipv4_addr_.sin_port != b.ipv4_addr_.sin_port) {
|
|
|
|
return a.ipv4_addr_.sin_port < b.ipv4_addr_.sin_port;
|
|
|
|
}
|
|
|
|
return std::memcmp(&a.ipv4_addr_.sin_addr, &b.ipv4_addr_.sin_addr, sizeof(a.ipv4_addr_.sin_addr)) < 0;
|
|
|
|
} else if (a.get_address_family() == AF_INET6) {
|
|
|
|
if (a.ipv6_addr_.sin6_port != b.ipv6_addr_.sin6_port) {
|
|
|
|
return a.ipv6_addr_.sin6_port < b.ipv6_addr_.sin6_port;
|
|
|
|
}
|
|
|
|
return std::memcmp(&a.ipv6_addr_.sin6_addr, &b.ipv6_addr_.sin6_addr, sizeof(a.ipv6_addr_.sin6_addr)) < 0;
|
|
|
|
}
|
|
|
|
|
2020-07-13 20:49:07 +02:00
|
|
|
UNREACHABLE();
|
2018-12-31 20:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &builder, const IPAddress &address) {
|
|
|
|
if (!address.is_valid()) {
|
|
|
|
return builder << "[invalid]";
|
|
|
|
}
|
2020-05-16 22:12:52 +02:00
|
|
|
return builder << "[" << address.get_ip_host() << ":" << address.get_port() << "]";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|