Add IPAddress::get_ip_address.

GitOrigin-RevId: 8b82c462f10d705412cb6cedc3d25d5fb95c59e2
This commit is contained in:
levlam 2020-05-16 17:35:53 +03:00
parent 705ab4d415
commit e58d423af1
3 changed files with 14 additions and 6 deletions

View File

@ -34,12 +34,7 @@ class GoogleDnsResolver : public Actor {
double begin_time_ = 0; double begin_time_ = 0;
void start_up() override { void start_up() override {
auto r_address = IPAddress::get_ipv4_address(host_); auto r_address = IPAddress::get_ip_address(host_);
if (r_address.is_ok()) {
promise_.set_value(r_address.move_as_ok());
return stop();
}
r_address = IPAddress::get_ipv6_address(host_);
if (r_address.is_ok()) { if (r_address.is_ok()) {
promise_.set_value(r_address.move_as_ok()); promise_.set_value(r_address.move_as_ok());
return stop(); return stop();

View File

@ -350,6 +350,18 @@ Status IPAddress::init_ipv4_port(CSlice ipv4, int port) {
return Status::OK(); return Status::OK();
} }
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();
}
return Status::Error("Not a valid IP address");
}
Result<IPAddress> IPAddress::get_ipv4_address(CSlice host) { Result<IPAddress> IPAddress::get_ipv4_address(CSlice host) {
// sometimes inet_addr allows much more valid IPv4 hosts than inet_pton, // sometimes inet_addr allows much more valid IPv4 hosts than inet_pton,
// like 0x12.0x34.0x56.0x78, or 0x12345678, or 0x7f.001 // like 0x12.0x34.0x56.0x78, or 0x12345678, or 0x7f.001

View File

@ -51,6 +51,7 @@ class IPAddress {
static Result<IPAddress> get_ipv4_address(CSlice host); static Result<IPAddress> get_ipv4_address(CSlice host);
static Result<IPAddress> get_ipv6_address(CSlice host); static Result<IPAddress> get_ipv6_address(CSlice host);
static Result<IPAddress> get_ip_address(CSlice host); // host must be any IPv4 or IPv6
Status init_ipv6_port(CSlice ipv6, int port) TD_WARN_UNUSED_RESULT; Status init_ipv6_port(CSlice ipv6, int port) TD_WARN_UNUSED_RESULT;
Status init_ipv6_as_ipv4_port(CSlice ipv4, int port) TD_WARN_UNUSED_RESULT; Status init_ipv6_as_ipv4_port(CSlice ipv4, int port) TD_WARN_UNUSED_RESULT;