0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 07:23:53 +01:00

ircd::net::dns: Reorg various utilities from dns::cache:: to dns::.

This commit is contained in:
Jason Volk 2019-03-24 14:50:26 -07:00
parent 55af53e2a0
commit 71b86df0fe
3 changed files with 100 additions and 100 deletions

View file

@ -30,7 +30,13 @@ namespace ircd::net::dns
void resolve(const hostport &, const opts &, callback_one); // convenience
void resolve(const hostport &, const opts &, callback_ipport); // convenience
// (internal) generate strings for rfc1035 questions or dns::cache keys.
// Utilities
bool is_error(const json::object &rr);
bool is_error(const json::array &rr);
time_t get_ttl(const json::object &rr);
bool expired(const json::object &rr, const time_t &rr_ts, const time_t &min_ttl);
bool expired(const json::object &rr, const time_t &rr_ts);
json::object random_choice(const json::array &);
string_view make_SRV_key(const mutable_buffer &out, const hostport &, const opts &);
string_view unmake_SRV_key(const string_view &);
@ -83,12 +89,6 @@ namespace ircd::net::dns::cache
{
using closure = std::function<bool (const string_view &, const json::object &)>;
bool is_error(const json::object &rr);
bool is_error(const json::array &rr);
time_t get_ttl(const json::object &rr);
bool expired(const json::object &rr, const time_t &rr_ts, const time_t &min_ttl);
bool expired(const json::object &rr, const time_t &rr_ts);
json::object random_choice(const json::array &);
string_view make_type(const mutable_buffer &out, const string_view &);
string_view make_type(const mutable_buffer &out, const uint16_t &);

View file

@ -3358,6 +3358,98 @@ ircd::net::dns::make_SRV_key(const mutable_buffer &out,
};
}
ircd::json::object
ircd::net::dns::random_choice(const json::array &rrs)
{
const size_t &count
{
rrs.size()
};
if(!count)
return json::object{};
const auto choice
{
rand::integer(0, count - 1)
};
assert(choice < count);
const json::object &rr
{
rrs[choice]
};
return rr;
}
bool
ircd::net::dns::expired(const json::object &rr,
const time_t &rr_ts)
{
static mods::import<conf::item<seconds>> min_ttl
{
"s_dns", "ircd::net::dns::cache::min_ttl"
};
static mods::import<conf::item<seconds>> error_ttl
{
"s_dns", "ircd::net::dns::cache::error_ttl"
};
const conf::item<seconds> &min_ttl_item
{
static_cast<conf::item<seconds> &>(min_ttl)
};
const conf::item<seconds> &error_ttl_item
{
static_cast<conf::item<seconds> &>(error_ttl)
};
const seconds &min_seconds(min_ttl_item);
const seconds &err_seconds(error_ttl_item);
const time_t &min
{
is_error(rr)?
err_seconds.count():
min_seconds.count()
};
return expired(rr, rr_ts, min);
}
bool
ircd::net::dns::expired(const json::object &rr,
const time_t &rr_ts,
const time_t &min_ttl)
{
const auto ttl(get_ttl(rr));
return rr_ts + std::max(ttl, min_ttl) < ircd::time();
}
time_t
ircd::net::dns::get_ttl(const json::object &rr)
{
return rr.get<time_t>("ttl", 0L);
}
bool
ircd::net::dns::is_error(const json::array &rrs)
{
return !std::none_of(begin(rrs), end(rrs), []
(const json::object &rr)
{
return is_error(rr);
});
}
bool
ircd::net::dns::is_error(const json::object &rr)
{
return rr.has("error");
}
//
// cache
//
@ -3507,98 +3599,6 @@ ircd::net::dns::cache::make_type(const mutable_buffer &out,
};
}
ircd::json::object
ircd::net::dns::cache::random_choice(const json::array &rrs)
{
const size_t &count
{
rrs.size()
};
if(!count)
return json::object{};
const auto choice
{
rand::integer(0, count - 1)
};
assert(choice < count);
const json::object &rr
{
rrs[choice]
};
return rr;
}
bool
ircd::net::dns::cache::expired(const json::object &rr,
const time_t &rr_ts)
{
static mods::import<conf::item<seconds>> min_ttl
{
"s_dns", "ircd::net::dns::cache::min_ttl"
};
static mods::import<conf::item<seconds>> error_ttl
{
"s_dns", "ircd::net::dns::cache::error_ttl"
};
const conf::item<seconds> &min_ttl_item
{
static_cast<conf::item<seconds> &>(min_ttl)
};
const conf::item<seconds> &error_ttl_item
{
static_cast<conf::item<seconds> &>(error_ttl)
};
const seconds &min_seconds(min_ttl_item);
const seconds &err_seconds(error_ttl_item);
const time_t &min
{
is_error(rr)?
err_seconds.count():
min_seconds.count()
};
return expired(rr, rr_ts, min);
}
bool
ircd::net::dns::cache::expired(const json::object &rr,
const time_t &rr_ts,
const time_t &min_ttl)
{
const auto ttl(get_ttl(rr));
return rr_ts + std::max(ttl, min_ttl) < ircd::time();
}
time_t
ircd::net::dns::cache::get_ttl(const json::object &rr)
{
return rr.get<time_t>("ttl", 0L);
}
bool
ircd::net::dns::cache::is_error(const json::array &rrs)
{
return !std::none_of(begin(rrs), end(rrs), []
(const json::object &rr)
{
return is_error(rr);
});
}
bool
ircd::net::dns::cache::is_error(const json::object &rr)
{
return rr.has("error");
}
///////////////////////////////////////////////////////////////////////////////
//
// net/ipport.h

View file

@ -112,7 +112,7 @@ ircd::net::dns::handle_resolve_one(const hostport &hp,
{
const json::object &rr
{
cache::random_choice(rrs)
random_choice(rrs)
};
callback(hp, rr);