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

Commit bad8b882 authored by chenbruce's avatar chenbruce
Browse files

Setting a DnsQueryEvent for mDNS

When .local resolution is working in the field, identifying the query
as being multicast.

Bug: 140857615
Test: cd packages/modules/DnsResolver && atest
Change-Id: I809967e0899030c05dce4c03a15fee7cf8e7ab26
parent ce61df39
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -139,6 +139,7 @@ using android::net::PrivateDnsConfiguration;
using android::net::PrivateDnsMode;
using android::net::PrivateDnsModes;
using android::net::PrivateDnsStatus;
using android::net::PROTO_MDNS;
using android::net::PROTO_TCP;
using android::net::PROTO_UDP;
using android::netdutils::IPSockAddr;
@@ -462,7 +463,19 @@ int res_nsend(res_state statp, const uint8_t* buf, int buflen, uint8_t* ans, int
        int resplen = 0;
        *rcode = RCODE_INTERNAL_ERROR;
        std::span<const uint8_t> buffer(buf, buflen);
        Stopwatch queryStopwatch;
        resplen = send_mdns(statp, buffer, ans, anssiz, &terrno, rcode);
        const IPSockAddr& receivedMdnsAddr =
                (getQueryType(buf, buflen) == T_AAAA) ? mdns_addrs[0] : mdns_addrs[1];
        DnsQueryEvent* mDnsQueryEvent = addDnsQueryEvent(statp->event);
        mDnsQueryEvent->set_cache_hit(static_cast<CacheStatus>(cache_status));
        mDnsQueryEvent->set_latency_micros(saturate_cast<int32_t>(queryStopwatch.timeTakenUs()));
        mDnsQueryEvent->set_ip_version(ipFamilyToIPVersion(receivedMdnsAddr.family()));
        mDnsQueryEvent->set_rcode(static_cast<NsRcode>(*rcode));
        mDnsQueryEvent->set_protocol(PROTO_MDNS);
        mDnsQueryEvent->set_type(getQueryType(buf, buflen));
        mDnsQueryEvent->set_linux_errno(static_cast<LinuxErrno>(terrno));
        resolv_stats_add(statp->netid, receivedMdnsAddr, mDnsQueryEvent);

        if (resplen <= 0) {
            _resolv_cache_query_failed(statp->netid, buf, buflen, flags);
+1 −0
Original line number Diff line number Diff line
@@ -160,6 +160,7 @@ enum Protocol {
    PROTO_TCP = 2;
    PROTO_DOT = 3;
    PROTO_DOH = 4;
    PROTO_MDNS = 5;
}

enum PrivateDnsModes {
+130 −5
Original line number Diff line number Diff line
@@ -924,6 +924,80 @@ TEST_F(ResolvGetAddrInfoTest, MdnsAlphabeticalHostname) {
    constexpr char v4addr[] = "127.0.0.3";
    constexpr char v6addr[] = "::127.0.0.3";
    constexpr char host_name[] = "hello.local.";
    // Following fields will not be verified during the test in proto NetworkDnsEventReported.
    // So don't need to config those values: event_type, return_code, latency_micros,
    // hints_ai_flags, res_nsend_flags, network_type, private_dns_modes.

    constexpr char event_ipv4[] = R"Event(
             NetworkDnsEventReported {
             dns_query_events:
             {
               dns_query_event:[
                {
                 rcode: 0,
                 type: 1,
                 cache_hit: 1,
                 ip_version: 1,
                 protocol: 5,
                 retry_times: 0,
                 dns_server_index: 0,
                 connected: 0,
                 linux_errno: 0,
                },
               ]
             }
        })Event";

    constexpr char event_ipv6[] = R"Event(
             NetworkDnsEventReported {
             dns_query_events:
             {
               dns_query_event:[
                {
                 rcode: 0,
                 type: 28,
                 cache_hit: 1,
                 ip_version: 2,
                 protocol: 5,
                 retry_times: 0,
                 dns_server_index: 0,
                 connected: 0,
                 linux_errno: 0,
                },
               ]
             }
        })Event";

    constexpr char event_ipv4v6[] = R"Event(
             NetworkDnsEventReported {
             dns_query_events:
             {
               dns_query_event:[
                {
                 rcode: 0,
                 type: 28,
                 cache_hit: 1,
                 ip_version: 2,
                 protocol: 5,
                 retry_times: 0,
                 dns_server_index: 0,
                 connected: 0,
                 linux_errno: 0,
                },
                {
                 rcode: 0,
                 type: 1,
                 cache_hit: 1,
                 ip_version: 1,
                 protocol: 5,
                 retry_times: 0,
                 dns_server_index: 0,
                 connected: 0,
                 linux_errno: 0,
                }
               ]
             }
        })Event";

    test::DNSResponder mdnsv4("127.0.0.3", test::kDefaultMdnsListenService);
    test::DNSResponder mdnsv6("::1", test::kDefaultMdnsListenService);
@@ -936,10 +1010,11 @@ TEST_F(ResolvGetAddrInfoTest, MdnsAlphabeticalHostname) {
    static const struct TestConfig {
        int ai_family;
        const std::vector<std::string> expected_addr;
        const std::string expected_event;
    } testConfigs[]{
            {AF_UNSPEC, {v4addr, v6addr}},
            {AF_INET, {v4addr}},
            {AF_INET6, {v6addr}},
            {AF_UNSPEC, {v4addr, v6addr}, event_ipv4v6},
            {AF_INET, {v4addr}, event_ipv4},
            {AF_INET6, {v6addr}, event_ipv6},
    };

    for (const auto& config : testConfigs) {
@@ -951,6 +1026,8 @@ TEST_F(ResolvGetAddrInfoTest, MdnsAlphabeticalHostname) {
        const addrinfo hints = {.ai_family = config.ai_family, .ai_socktype = SOCK_DGRAM};
        NetworkDnsEventReported event;
        int rv = resolv_getaddrinfo("hello.local", nullptr, &hints, &mNetcontext, &result, &event);
        EXPECT_THAT(event,
                    NetworkDnsEventEq(fromNetworkDnsEventReportedStr(config.expected_event)));
        ScopedAddrinfo result_cleanup(result);

        if (config.ai_family == AF_UNSPEC) {
@@ -1671,6 +1748,51 @@ TEST_F(GetHostByNameForNetContextTest, MdnsAlphabeticalHostname) {
    constexpr char v6addr[] = "::127.0.0.3";
    constexpr char host_name[] = "hello.local.";

    // Following fields will not be verified during the test in proto NetworkDnsEventReported.
    // So don't need to config those values: event_type, return_code, latency_micros,
    // hints_ai_flags, res_nsend_flags, network_type, private_dns_modes.
    constexpr char event_ipv4[] = R"Event(
             NetworkDnsEventReported {
             dns_query_events:
             {
               dns_query_event:[
                {
                 rcode: 0,
                 type: 1,
                 cache_hit: 1,
                 ip_version: 1,
                 protocol: 5,
                 retry_times: 0,
                 dns_server_index: 0,
                 connected: 0,
                 latency_micros: 0,
                 linux_errno: 0,
                }
               ]
             }
        })Event";

    constexpr char event_ipv6[] = R"Event(
             NetworkDnsEventReported {
             dns_query_events:
             {
               dns_query_event:[
                {
                 rcode: 0,
                 type: 28,
                 cache_hit: 1,
                 ip_version: 2,
                 protocol: 5,
                 retry_times: 0,
                 dns_server_index: 0,
                 connected: 0,
                 latency_micros: 0,
                 linux_errno: 0,
                }
               ]
             }
        })Event";

    test::DNSResponder mdnsv4("127.0.0.3", test::kDefaultMdnsListenService);
    test::DNSResponder mdnsv6("::1", test::kDefaultMdnsListenService);

@@ -1684,9 +1806,10 @@ TEST_F(GetHostByNameForNetContextTest, MdnsAlphabeticalHostname) {
    static const struct TestConfig {
        int ai_family;
        const std::vector<std::string> expected_addr;
        const std::string expected_event;
    } testConfigs[]{
            {AF_INET, {v4addr}},
            {AF_INET6, {v6addr}},
            {AF_INET, {v4addr}, event_ipv4},
            {AF_INET6, {v6addr}, event_ipv6},
    };

    for (const auto& config : testConfigs) {
@@ -1697,6 +1820,8 @@ TEST_F(GetHostByNameForNetContextTest, MdnsAlphabeticalHostname) {
        NetworkDnsEventReported event;
        int rv = resolv_gethostbyname("hello.local", config.ai_family, &hbuf, tmpbuf,
                                      sizeof(tmpbuf), &mNetcontext, &result, &event);
        EXPECT_THAT(event,
                    NetworkDnsEventEq(fromNetworkDnsEventReportedStr(config.expected_event)));
        EXPECT_EQ(0, rv);
        test::DNSResponder& mdns = config.ai_family == AF_INET ? mdnsv4 : mdnsv6;
        EXPECT_EQ(1U, GetNumQueries(mdns, host_name));