Merge "Minor style fixes"
diff --git a/tests/dns_responder/dns_responder.cpp b/tests/dns_responder/dns_responder.cpp
index caa0072..51eb7dd 100644
--- a/tests/dns_responder/dns_responder.cpp
+++ b/tests/dns_responder/dns_responder.cpp
@@ -459,11 +459,11 @@
std::lock_guard lock(mappings_mutex_);
auto it = mappings_.find(QueryKey(name, type));
if (it != mappings_.end()) {
- LOG(ERROR) << "Cannot remove mapping mapping from (" << name << ", " << dnstype2str(type)
- << "), not present";
+ mappings_.erase(it);
return;
}
- mappings_.erase(it);
+ LOG(ERROR) << "Cannot remove mapping from (" << name << ", " << dnstype2str(type)
+ << "), not present";
}
void DNSResponder::setResponseProbability(double response_probability) {
@@ -667,24 +667,9 @@
}
}
- for (const DNSQuestion& question : header.questions) {
- if (question.qclass != ns_class::ns_c_in && question.qclass != ns_class::ns_c_any) {
- LOG(INFO) << "unsupported question class " << question.qclass;
- return makeErrorResponse(&header, ns_rcode::ns_r_notimpl, response, response_len);
- }
-
- if (!addAnswerRecords(question, &header.answers)) {
- return makeErrorResponse(&header, ns_rcode::ns_r_servfail, response, response_len);
- }
- }
-
- header.qr = true;
- char* response_cur = header.write(response, response + *response_len);
- if (response_cur == nullptr) {
- return false;
- }
- *response_len = response_cur - response;
- return true;
+ // Make the response. The query has been read into |header| which is used to build and return
+ // the response as well.
+ return makeResponse(&header, response, response_len);
}
bool DNSResponder::addAnswerRecords(const DNSQuestion& question,
@@ -796,6 +781,27 @@
return true;
}
+bool DNSResponder::makeResponse(DNSHeader* header, char* response, size_t* response_len) const {
+ for (const DNSQuestion& question : header->questions) {
+ if (question.qclass != ns_class::ns_c_in && question.qclass != ns_class::ns_c_any) {
+ LOG(INFO) << "unsupported question class " << question.qclass;
+ return makeErrorResponse(header, ns_rcode::ns_r_notimpl, response, response_len);
+ }
+
+ if (!addAnswerRecords(question, &header->answers)) {
+ return makeErrorResponse(header, ns_rcode::ns_r_servfail, response, response_len);
+ }
+ }
+
+ header->qr = true;
+ char* response_cur = header->write(response, response + *response_len);
+ if (response_cur == nullptr) {
+ return false;
+ }
+ *response_len = response_cur - response;
+ return true;
+}
+
void DNSResponder::setDeferredResp(bool deferred_resp) {
std::lock_guard<std::mutex> guard(cv_mutex_for_deferred_resp_);
deferred_resp_ = deferred_resp;
diff --git a/tests/dns_responder/dns_responder.h b/tests/dns_responder/dns_responder.h
index 2ef8da0..41e7312 100644
--- a/tests/dns_responder/dns_responder.h
+++ b/tests/dns_responder/dns_responder.h
@@ -116,6 +116,13 @@
*/
class DNSResponder {
public:
+ enum class Edns : uint8_t {
+ ON,
+ FORMERR_ON_EDNS, // DNS server not supporting EDNS will reply FORMERR.
+ FORMERR_UNCOND, // DNS server reply FORMERR unconditionally
+ DROP // DNS server not supporting EDNS will not do any response.
+ };
+
DNSResponder(std::string listen_address = kDefaultListenAddr,
std::string listen_service = kDefaultListenService,
ns_rcode error_rcode = ns_rcode::ns_r_servfail);
@@ -125,13 +132,6 @@
~DNSResponder();
- enum class Edns : uint8_t {
- ON,
- FORMERR_ON_EDNS, // DNS server not supporting EDNS will reply FORMERR.
- FORMERR_UNCOND, // DNS server reply FORMERR unconditionally
- DROP // DNS server not supporting EDNS will not do any response.
- };
-
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);
@@ -184,6 +184,7 @@
size_t* response_len) const;
bool makeErrorResponse(DNSHeader* header, ns_rcode rcode, char* response,
size_t* response_len) const;
+ bool makeResponse(DNSHeader* header, char* response, size_t* response_len) const;
// Add a new file descriptor to be polled by the handler thread.
bool addFd(int fd, uint32_t events);