Small cleanups for DNSResponder
- use C++17's try_emplace() to simplify zero-copy map insertion
- pass around strings instead of naked char pointers
Test: atest netd_integration_test
Change-Id: If6f3b1792bb45204a36022231321812d85f5e5c0
diff --git a/tests/dns_responder/dns_responder.cpp b/tests/dns_responder/dns_responder.cpp
index 0e2989b..79ebc1d 100644
--- a/tests/dns_responder/dns_responder.cpp
+++ b/tests/dns_responder/dns_responder.cpp
@@ -536,28 +536,25 @@
stopServer();
}
-void DNSResponder::addMapping(const char* name, ns_type type,
- const char* addr) {
+void DNSResponder::addMapping(const std::string& name, ns_type type, const std::string& addr) {
std::lock_guard lock(mappings_mutex_);
auto it = mappings_.find(QueryKey(name, type));
if (it != mappings_.end()) {
ALOGI("Overwriting mapping for (%s, %s), previous address %s, new "
- "address %s", name, dnstype2str(type), it->second.c_str(),
- addr);
+ "address %s",
+ name.c_str(), dnstype2str(type), it->second.c_str(), addr.c_str());
it->second = addr;
return;
}
- mappings_.emplace(std::piecewise_construct,
- std::forward_as_tuple(name, type),
- std::forward_as_tuple(addr));
+ mappings_.try_emplace({name, type}, addr);
}
-void DNSResponder::removeMapping(const char* name, ns_type type) {
+void DNSResponder::removeMapping(const std::string& name, ns_type type) {
std::lock_guard lock(mappings_mutex_);
auto it = mappings_.find(QueryKey(name, type));
if (it != mappings_.end()) {
- ALOGI("Cannot remove mapping mapping from (%s, %s), not present", name,
- dnstype2str(type));
+ ALOGI("Cannot remove mapping mapping from (%s, %s), not present", name.c_str(),
+ dnstype2str(type));
return;
}
mappings_.erase(it);
@@ -867,4 +864,3 @@
}
} // namespace test
-
diff --git a/tests/dns_responder/dns_responder.h b/tests/dns_responder/dns_responder.h
index ec5c251..e3da72d 100644
--- a/tests/dns_responder/dns_responder.h
+++ b/tests/dns_responder/dns_responder.h
@@ -45,8 +45,8 @@
DNSResponder(std::string listen_address, std::string listen_service, int poll_timeout_ms,
ns_rcode error_rcode);
~DNSResponder();
- void addMapping(const char* name, ns_type type, const char* addr);
- void removeMapping(const char* name, ns_type type);
+ void addMapping(const std::string& name, ns_type type, const std::string& addr);
+ void removeMapping(const std::string& name, ns_type type);
void setResponseProbability(double response_probability);
void setFailOnEdns(bool fail) { fail_on_edns_ = fail; }
bool running() const;
@@ -67,7 +67,8 @@
struct QueryKey {
std::string name;
unsigned type;
- QueryKey(std::string n, unsigned t) : name(n), type(t) {}
+
+ QueryKey(std::string n, unsigned t) : name(move(n)), type(t) {}
bool operator == (const QueryKey& o) const {
return name == o.name && type == o.type;
}