Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c8913992 authored by Hungming Chen's avatar Hungming Chen Committed by android-build-merger
Browse files

dns_responder: Wrap the response build into a function

am: d0678a10

Change-Id: I4e0485624ea6d7bb828a85e7fd0426a73b3296bf
parents 391a2ce6 d0678a10
Loading
Loading
Loading
Loading
+27 −21
Original line number Diff line number Diff line
@@ -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) {
@@ -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,
@@ -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;
+8 −7
Original line number Diff line number Diff line
@@ -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);
@@ -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);
@@ -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);