From 7f5f07509a5b119829f65701458204f0273f05fc Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 24 Mar 2019 20:12:15 -0700 Subject: [PATCH] ircd::net::dns: Add utils to properly check for empty records in JSON. --- include/ircd/net/dns.h | 2 ++ ircd/net.cc | 16 ++++++++++++++++ ircd/server.cc | 2 +- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/ircd/net/dns.h b/include/ircd/net/dns.h index 5b337cbcb..4835e7ec4 100644 --- a/include/ircd/net/dns.h +++ b/include/ircd/net/dns.h @@ -33,6 +33,8 @@ namespace ircd::net::dns // Utilities bool is_error(const json::object &rr); bool is_error(const json::array &rr); + bool is_empty(const json::object &rr); + bool is_empty(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); diff --git a/ircd/net.cc b/ircd/net.cc index d9b315287..3db105b75 100644 --- a/ircd/net.cc +++ b/ircd/net.cc @@ -3456,6 +3456,22 @@ ircd::net::dns::get_ttl(const json::object &rr) return rr.get("ttl", 0L); } +bool +ircd::net::dns::is_empty(const json::array &rrs) +{ + return std::all_of(begin(rrs), end(rrs), [] + (const json::object &rr) + { + return is_empty(rr); + }); +} + +bool +ircd::net::dns::is_empty(const json::object &rr) +{ + return empty(rr) || (rr.has("ttl") && size(rr) == 1); +} + bool ircd::net::dns::is_error(const json::array &rrs) { diff --git a/ircd/server.cc b/ircd/server.cc index ea597a34c..c2493abe0 100644 --- a/ircd/server.cc +++ b/ircd/server.cc @@ -1080,7 +1080,7 @@ try if(op_fini) return; - if(empty(rrs)) + if(net::dns::is_empty(rrs)) { err_set(make_exception_ptr("Host has no address record.")); assert(this->e && this->e->eptr);