From 71b86df0fe2b7f684e65ca3739ae6a8b3ac94f77 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 24 Mar 2019 14:50:26 -0700 Subject: [PATCH] ircd::net::dns: Reorg various utilities from dns::cache:: to dns::. --- include/ircd/net/dns.h | 14 ++-- ircd/net.cc | 184 ++++++++++++++++++++--------------------- modules/s_dns.cc | 2 +- 3 files changed, 100 insertions(+), 100 deletions(-) diff --git a/include/ircd/net/dns.h b/include/ircd/net/dns.h index 151203ae2..5b337cbcb 100644 --- a/include/ircd/net/dns.h +++ b/include/ircd/net/dns.h @@ -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 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 &); diff --git a/ircd/net.cc b/ircd/net.cc index 77296492f..0334a9db7 100644 --- a/ircd/net.cc +++ b/ircd/net.cc @@ -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> min_ttl + { + "s_dns", "ircd::net::dns::cache::min_ttl" + }; + + static mods::import> error_ttl + { + "s_dns", "ircd::net::dns::cache::error_ttl" + }; + + const conf::item &min_ttl_item + { + static_cast &>(min_ttl) + }; + + const conf::item &error_ttl_item + { + static_cast &>(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("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> min_ttl - { - "s_dns", "ircd::net::dns::cache::min_ttl" - }; - - static mods::import> error_ttl - { - "s_dns", "ircd::net::dns::cache::error_ttl" - }; - - const conf::item &min_ttl_item - { - static_cast &>(min_ttl) - }; - - const conf::item &error_ttl_item - { - static_cast &>(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("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 diff --git a/modules/s_dns.cc b/modules/s_dns.cc index a071eac4f..b2873972c 100644 --- a/modules/s_dns.cc +++ b/modules/s_dns.cc @@ -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);