diff options
author | ADAM David Alan Martin <adam.martin@10gen.com> | 2018-02-09 16:35:52 -0500 |
---|---|---|
committer | ADAM David Alan Martin <adam.martin@10gen.com> | 2018-02-09 16:35:52 -0500 |
commit | bf33c24ff0cf4018dfa94e3a349421ace28d1fac (patch) | |
tree | 0cb96460c56912b61093289cd713703d485ad13e /src/mongo/util/dns_query.cpp | |
parent | fdc23cf6a586a607299ec8a880574e95071e8f39 (diff) | |
download | mongo-bf33c24ff0cf4018dfa94e3a349421ace28d1fac.tar.gz |
SERVER-33140 DNS must disregard irrelevant records
DNS Queries can return superfluous records of non-requested type in
Windows DNSApi calls. Windows implementations will now ignore
records of those types.
Diffstat (limited to 'src/mongo/util/dns_query.cpp')
-rw-r--r-- | src/mongo/util/dns_query.cpp | 66 |
1 files changed, 47 insertions, 19 deletions
diff --git a/src/mongo/util/dns_query.cpp b/src/mongo/util/dns_query.cpp index 7da5909b0cd..5d10b2fb82e 100644 --- a/src/mongo/util/dns_query.cpp +++ b/src/mongo/util/dns_query.cpp @@ -43,6 +43,8 @@ #include <boost/noncopyable.hpp> +#include "mongo/bson/util/builder.h" + // It is safe to include the implementation "headers" in an anonymous namespace, as the code is // meant to live in a single TU -- this one. Include one of these headers last. #define MONGO_UTIL_DNS_QUERY_PLATFORM_INCLUDE_WHITELIST @@ -68,15 +70,25 @@ std::vector<std::string> dns::lookupARecords(const std::string& service) { DNSQueryState dnsQuery; auto response = dnsQuery.lookup(service, DNSQueryClass::kInternet, DNSQueryType::kAddress); - if (response.size() == 0) { - uasserted(ErrorCodes::DNSProtocolError, - "Looking up " + service + " A record yielded no results."); + std::vector<std::string> rv; + + for (const auto& entry : response) { + try { + rv.push_back(entry.addressEntry()); + } catch (const ExceptionFor<ErrorCodes::DNSRecordTypeMismatch>&) { + } } - std::vector<std::string> rv; - std::transform(begin(response), end(response), back_inserter(rv), [](const auto& entry) { - return entry.addressEntry(); - }); + if (rv.empty()) { + StringBuilder oss; + oss << "Looking up " << service << " A record yielded "; + if (response.size() == 0) { + oss << "no results."; + } else { + oss << "no A records but " << response.size() << " other records"; + } + uasserted(ErrorCodes::DNSProtocolError, oss.str()); + } return rv; } @@ -88,9 +100,24 @@ std::vector<dns::SRVHostEntry> dns::lookupSRVRecords(const std::string& service) std::vector<SRVHostEntry> rv; - std::transform(begin(response), end(response), back_inserter(rv), [](const auto& entry) { - return entry.srvHostEntry(); - }); + for (const auto& entry : response) { + try { + rv.push_back(entry.srvHostEntry()); + } catch (const ExceptionFor<ErrorCodes::DNSRecordTypeMismatch>&) { + } + } + + if (rv.empty()) { + StringBuilder oss; + oss << "Looking up " << service << " SRV record yielded "; + if (response.size() == 0) { + oss << "no results."; + } else { + oss << "no SRV records but " << response.size() << " other records"; + } + uasserted(ErrorCodes::DNSProtocolError, oss.str()); + } + return rv; } @@ -102,20 +129,21 @@ std::vector<std::string> dns::lookupTXTRecords(const std::string& service) { std::vector<std::string> rv; for (auto& entry : response) { - auto txtEntry = entry.txtEntry(); - rv.insert(end(rv), - std::make_move_iterator(begin(txtEntry)), - std::make_move_iterator(end(txtEntry))); + try { + auto txtEntry = entry.txtEntry(); + rv.insert(end(rv), + std::make_move_iterator(begin(txtEntry)), + std::make_move_iterator(end(txtEntry))); + } catch (const ExceptionFor<ErrorCodes::DNSRecordTypeMismatch>&) { + } } + return rv; } std::vector<std::string> dns::getTXTRecords(const std::string& service) try { return lookupTXTRecords(service); -} catch (const DBException& ex) { - if (ex.code() == ErrorCodes::DNSHostNotFound) { - return {}; - } - throw; +} catch (const ExceptionFor<ErrorCodes::DNSHostNotFound>&) { + return {}; } } // namespace mongo |