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

Commit 6c186345 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "use std::span for res_pquery"

parents 3f5ee02f 2d831c5e
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -1199,7 +1199,7 @@ static void _cache_remove_oldest(Cache* cache) {
        return;
    }
    LOG(INFO) << __func__ << ": Cache full - removing oldest";
    res_pquery(oldest->query, oldest->querylen);
    res_pquery({oldest->query, oldest->querylen});
    _cache_remove_p(cache, lookup);
}

@@ -1304,7 +1304,7 @@ ResolvCacheStatus resolv_cache_lookup(unsigned netid, const void* query, int que
    /* remove stale entries here */
    if (now >= e->expires) {
        LOG(INFO) << __func__ << ": NOT IN CACHE (STALE ENTRY " << *lookup << "DISCARDED)";
        res_pquery(e->query, e->querylen);
        res_pquery({e->query, e->querylen});
        _cache_remove_p(cache, lookup);
        return RESOLV_CACHE_NOTFOUND;
    }
+16 −5
Original line number Diff line number Diff line
@@ -114,7 +114,6 @@
#include <inttypes.h>
#include <math.h>
#include <netdb.h>
#include <netdutils/Slice.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
@@ -132,7 +131,6 @@
#endif

using android::base::StringAppendF;
using android::netdutils::Slice;

struct res_sym {
    int number;            /* Identifying number, like T_MX */
@@ -233,18 +231,31 @@ static void do_section(ns_msg* handle, ns_sect section) {
    }
}

// Convert bytes to its hexadecimal representation.
// The returned string is double the size of input.
std::string bytesToHexStr(std::span<const uint8_t> bytes) {
    static char const hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
                                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    std::string str;
    str.reserve(bytes.size() * 2);
    for (uint8_t ch : bytes) {
        str.append({hex[(ch & 0xf0) >> 4], hex[ch & 0xf]});
    }
    return str;
}

/*
 * Print the contents of a query.
 * This is intended to be primarily a debugging routine.
 */
void res_pquery(const uint8_t* msg, int len) {
void res_pquery(std::span<const uint8_t> msg) {
    if (!WOULD_LOG(VERBOSE)) return;

    ns_msg handle;
    int qdcount, ancount, nscount, arcount;
    uint32_t opcode, rcode, id;

    if (ns_initparse(msg, len, &handle) < 0) {
    if (ns_initparse(msg.data(), msg.size(), &handle) < 0) {
        PLOG(VERBOSE) << "ns_initparse failed";
        return;
    }
@@ -287,7 +298,7 @@ void res_pquery(const uint8_t* msg, int len) {
    do_section(&handle, ns_s_ar);

    LOG(VERBOSE) << "Hex dump:";
    LOG(VERBOSE) << android::netdutils::toHex(Slice(const_cast<uint8_t*>(msg), len), 32);
    LOG(VERBOSE) << bytesToHexStr(msg);
}

/*
+3 −3
Original line number Diff line number Diff line
@@ -17,10 +17,10 @@
#pragma once

#include <stdint.h>
#include <span>

// TODO: use netdutils::Slice for (msg, len).
void res_pquery(const uint8_t* msg, int len);

void res_pquery(std::span<const uint8_t> msg);
std::string bytesToHexStr(std::span<const uint8_t> bytes);
// Thread-unsafe functions returning pointers to static buffers :-(
// TODO: switch all res_debug to std::string
const char* p_type(int type);
+8 −8
Original line number Diff line number Diff line
@@ -443,7 +443,7 @@ int res_nsend(ResState* statp, const uint8_t* buf, int buflen, uint8_t* ans, int
        errno = EINVAL;
        return -EINVAL;
    }
    res_pquery(buf, buflen);
    res_pquery({buf, buflen});

    int anslen = 0;
    Stopwatch cacheStopwatch;
@@ -490,7 +490,7 @@ int res_nsend(ResState* statp, const uint8_t* buf, int buflen, uint8_t* ans, int
            return -terrno;
        }
        LOG(DEBUG) << __func__ << ": got answer:";
        res_pquery(ans, (resplen > anssiz) ? anssiz : resplen);
        res_pquery({ans, (resplen > anssiz) ? anssiz : resplen});

        if (cache_status == RESOLV_CACHE_NOTFOUND) {
            resolv_cache_add(statp->netid, buf, buflen, ans, resplen);
@@ -517,7 +517,7 @@ int res_nsend(ResState* statp, const uint8_t* buf, int buflen, uint8_t* ans, int
                                           Slice(ans, anssiz), rcode, &fallback);
        if (resplen > 0) {
            LOG(DEBUG) << __func__ << ": got answer from Private DNS";
            res_pquery(ans, resplen);
            res_pquery({ans, resplen});
            if (cache_status == RESOLV_CACHE_NOTFOUND) {
                resolv_cache_add(statp->netid, buf, buflen, ans, resplen);
            }
@@ -666,7 +666,7 @@ int res_nsend(ResState* statp, const uint8_t* buf, int buflen, uint8_t* ans, int
            }

            LOG(DEBUG) << __func__ << ": got answer:";
            res_pquery(ans, (resplen > anssiz) ? anssiz : resplen);
            res_pquery({ans, (resplen > anssiz) ? anssiz : resplen});

            if (cache_status == RESOLV_CACHE_NOTFOUND) {
                resolv_cache_add(statp->netid, buf, buflen, ans, resplen);
@@ -903,7 +903,7 @@ read_len:
     */
    if (hp->id != anhp->id) {
        LOG(DEBUG) << __func__ << ": ld answer (unexpected):";
        res_pquery(ans, resplen);
        res_pquery({ans, resplen});
        goto read_len;
    }

@@ -1168,7 +1168,7 @@ static int send_dg(ResState* statp, res_params* params, const uint8_t* buf, int
            if (needRetry =
                        ignoreInvalidAnswer(statp, from, buf, buflen, ans, anssiz, &receivedFromNs);
                needRetry) {
                res_pquery(ans, (resplen > anssiz) ? anssiz : resplen);
                res_pquery({ans, (resplen > anssiz) ? anssiz : resplen});
                continue;
            }

@@ -1178,7 +1178,7 @@ static int send_dg(ResState* statp, res_params* params, const uint8_t* buf, int
                //  The case has to be captured here, as FORMERR packet do not
                //  carry query section, hence res_queriesmatch() returns 0.
                LOG(DEBUG) << __func__ << ": server rejected query with EDNS0:";
                res_pquery(ans, (resplen > anssiz) ? anssiz : resplen);
                res_pquery({ans, (resplen > anssiz) ? anssiz : resplen});
                // record the error
                statp->flags |= RES_F_EDNS0ERR;
                *terrno = EREMOTEIO;
@@ -1189,7 +1189,7 @@ static int send_dg(ResState* statp, res_params* params, const uint8_t* buf, int
            *delay = res_stats_calculate_rtt(&done, &start_time);
            if (anhp->rcode == SERVFAIL || anhp->rcode == NOTIMP || anhp->rcode == REFUSED) {
                LOG(DEBUG) << __func__ << ": server rejected query:";
                res_pquery(ans, (resplen > anssiz) ? anssiz : resplen);
                res_pquery({ans, (resplen > anssiz) ? anssiz : resplen});
                *rcode = anhp->rcode;
                continue;
            }
+15 −16
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <span>

#include <chrono>
#include <iostream>
@@ -38,14 +39,12 @@
#include <android-base/strings.h>
#include <netdutils/BackoffSequence.h>
#include <netdutils/InternetAddresses.h>
#include <netdutils/Slice.h>
#include <netdutils/SocketOption.h>

using android::base::unique_fd;
using android::netdutils::BackoffSequence;
using android::netdutils::enableSockopt;
using android::netdutils::ScopedAddrinfo;
using android::netdutils::Slice;
using std::chrono::milliseconds;

namespace test {
@@ -57,17 +56,6 @@ std::string errno2str() {
    return strerror_r(errno, error_msg, sizeof(error_msg));
}

std::string str2hex(const char* buffer, size_t len) {
    std::string str(len * 2, '\0');
    for (size_t i = 0; i < len; ++i) {
        static const char* hex = "0123456789ABCDEF";
        uint8_t c = buffer[i];
        str[i * 2] = hex[c >> 4];
        str[i * 2 + 1] = hex[c & 0x0F];
    }
    return str;
}

std::string addr2str(const sockaddr* sa, socklen_t sa_len) {
    char host_str[NI_MAXHOST] = {0};
    int rv = getnameinfo(sa, sa_len, host_str, sizeof(host_str), nullptr, 0, NI_NUMERICHOST);
@@ -75,6 +63,17 @@ std::string addr2str(const sockaddr* sa, socklen_t sa_len) {
    return std::string();
}

std::string bytesToHexStr(std::span<const uint8_t> bytes) {
    static char const hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
                                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    std::string str;
    str.reserve(bytes.size() * 2);
    for (uint8_t ch : bytes) {
        str.append({hex[(ch & 0xf0) >> 4], hex[ch & 0xf]});
    }
    return str;
}

// Because The address might still being set up (b/186181084), This is a wrapper function
// that retries bind() if errno is EADDRNOTAVAIL
int bindSocket(int socket, const sockaddr* address, socklen_t address_len) {
@@ -536,8 +535,7 @@ void DNSResponder::removeMappingBinaryPacket(const std::vector<uint8_t>& query)
    if (!packet_mappings_.erase(query)) {
        LOG(ERROR) << "Cannot remove mapping, not present in registered BinaryPacket mappings";
        LOG(INFO) << "Hex dump:";
        LOG(INFO) << android::netdutils::toHex(
                Slice(const_cast<uint8_t*>(query.data()), query.size()), 32);
        LOG(INFO) << bytesToHexStr(query);
    }
}

@@ -737,7 +735,8 @@ void DNSResponder::requestHandler() {

bool DNSResponder::handleDNSRequest(const char* buffer, ssize_t len, int protocol, char* response,
                                    size_t* response_len) const {
    LOG(DEBUG) << "request: '" << str2hex(buffer, len) << "', on " << dnsproto2str(protocol);
    LOG(DEBUG) << "request: '" << bytesToHexStr({reinterpret_cast<const uint8_t*>(buffer), len})
               << "', on " << dnsproto2str(protocol);
    const char* buffer_end = buffer + len;
    DNSHeader header;
    const char* cur = header.read(buffer, buffer_end);