Loading tests/dns_responder/dns_responder.cpp +27 −21 Original line number Diff line number Diff line Loading @@ -459,11 +459,11 @@ 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()) { 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) { Loading Loading @@ -667,24 +667,9 @@ bool DNSResponder::handleDNSRequest(const char* buffer, ssize_t len, char* respo } } 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, Loading Loading @@ -796,6 +781,27 @@ bool DNSResponder::makeErrorResponse(DNSHeader* header, ns_rcode rcode, char* re 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; Loading tests/dns_responder/dns_responder.h +8 −7 Original line number Diff line number Diff line Loading @@ -116,6 +116,13 @@ inline const std::string kDefaultListenService = "53"; */ 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); Loading @@ -125,13 +132,6 @@ class DNSResponder { ~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); Loading Loading @@ -184,6 +184,7 @@ class DNSResponder { 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); Loading Loading
tests/dns_responder/dns_responder.cpp +27 −21 Original line number Diff line number Diff line Loading @@ -459,11 +459,11 @@ 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()) { 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) { Loading Loading @@ -667,24 +667,9 @@ bool DNSResponder::handleDNSRequest(const char* buffer, ssize_t len, char* respo } } 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, Loading Loading @@ -796,6 +781,27 @@ bool DNSResponder::makeErrorResponse(DNSHeader* header, ns_rcode rcode, char* re 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; Loading
tests/dns_responder/dns_responder.h +8 −7 Original line number Diff line number Diff line Loading @@ -116,6 +116,13 @@ inline const std::string kDefaultListenService = "53"; */ 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); Loading @@ -125,13 +132,6 @@ class DNSResponder { ~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); Loading Loading @@ -184,6 +184,7 @@ class DNSResponder { 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); Loading