Home | History | Annotate | Download | only in dns_responder
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "dns_responder_client"
     18 #include "dns_responder_client.h"
     19 
     20 #include <android-base/stringprintf.h>
     21 #include <utils/Log.h>
     22 
     23 // TODO: make this dynamic and stop depending on implementation details.
     24 #define TEST_OEM_NETWORK "oem29"
     25 #define TEST_NETID 30
     26 
     27 // TODO: move this somewhere shared.
     28 static const char* ANDROID_DNS_MODE = "ANDROID_DNS_MODE";
     29 
     30 using android::base::StringPrintf;
     31 
     32 void DnsResponderClient::SetupMappings(unsigned num_hosts, const std::vector<std::string>& domains,
     33         std::vector<Mapping>* mappings) {
     34     mappings->resize(num_hosts * domains.size());
     35     auto mappings_it = mappings->begin();
     36     for (unsigned i = 0 ; i < num_hosts ; ++i) {
     37         for (const auto& domain : domains) {
     38             mappings_it->host = StringPrintf("host%u", i);
     39             mappings_it->entry = StringPrintf("%s.%s.", mappings_it->host.c_str(),
     40                     domain.c_str());
     41             mappings_it->ip4 = StringPrintf("192.0.2.%u", i%253 + 1);
     42             mappings_it->ip6 = StringPrintf("2001:db8::%x", i%65534 + 1);
     43             ++mappings_it;
     44         }
     45     }
     46 }
     47 
     48 bool DnsResponderClient::SetResolversForNetwork(const std::vector<std::string>& servers,
     49         const std::vector<std::string>& domains, const std::vector<int>& params) {
     50     const auto rv = mNetdSrv->setResolverConfiguration(TEST_NETID, servers, domains, params,
     51             "", {}, {});
     52     return rv.isOk();
     53 }
     54 
     55 bool DnsResponderClient::SetResolversWithTls(const std::vector<std::string>& servers,
     56         const std::vector<std::string>& domains, const std::vector<int>& params,
     57         const std::vector<std::string>& tlsServers,
     58         const std::string& name, const std::vector<std::string>& fingerprints) {
     59     const auto rv = mNetdSrv->setResolverConfiguration(TEST_NETID, servers, domains, params,
     60             name, tlsServers, fingerprints);
     61     if (!rv.isOk()) ALOGI("SetResolversWithTls() -> %s", rv.toString8().c_str());
     62     return rv.isOk();
     63 }
     64 
     65 void DnsResponderClient::SetupDNSServers(unsigned num_servers, const std::vector<Mapping>& mappings,
     66         std::vector<std::unique_ptr<test::DNSResponder>>* dns,
     67         std::vector<std::string>* servers) {
     68     const char* listen_srv = "53";
     69     dns->resize(num_servers);
     70     servers->resize(num_servers);
     71     for (unsigned i = 0 ; i < num_servers ; ++i) {
     72         auto& server = (*servers)[i];
     73         auto& d = (*dns)[i];
     74         server = StringPrintf("127.0.0.%u", i + 100);
     75         d = std::make_unique<test::DNSResponder>(server, listen_srv, 250,
     76                 ns_rcode::ns_r_servfail, 1.0);
     77         for (const auto& mapping : mappings) {
     78             d->addMapping(mapping.entry.c_str(), ns_type::ns_t_a, mapping.ip4.c_str());
     79             d->addMapping(mapping.entry.c_str(), ns_type::ns_t_aaaa, mapping.ip6.c_str());
     80         }
     81         d->startServer();
     82     }
     83 }
     84 
     85 void DnsResponderClient::ShutdownDNSServers(std::vector<std::unique_ptr<test::DNSResponder>>* dns) {
     86     for (const auto& d : *dns) {
     87         d->stopServer();
     88     }
     89     dns->clear();
     90 }
     91 
     92 int DnsResponderClient::SetupOemNetwork() {
     93     mNetdSrv->networkDestroy(TEST_NETID);
     94     auto ret = mNetdSrv->networkCreatePhysical(TEST_NETID, "");
     95     if (!ret.isOk()) {
     96         fprintf(stderr, "Creating physical network %d failed, %s\n", TEST_NETID,
     97                 ret.toString8().string());
     98         return -1;
     99     }
    100     int oemNetId = TEST_NETID;
    101     setNetworkForProcess(oemNetId);
    102     if ((unsigned) oemNetId != getNetworkForProcess()) {
    103         return -1;
    104     }
    105     return oemNetId;
    106 }
    107 
    108 void DnsResponderClient::TearDownOemNetwork(int oemNetId) {
    109     if (oemNetId != -1) {
    110         mNetdSrv->networkDestroy(oemNetId);
    111     }
    112 }
    113 
    114 void DnsResponderClient::SetUp() {
    115     // binder setup
    116     auto binder = android::defaultServiceManager()->getService(android::String16("netd"));
    117     mNetdSrv = android::interface_cast<android::net::INetd>(binder);
    118 
    119     // Ensure resolutions go via proxy.
    120     setenv(ANDROID_DNS_MODE, "", 1);
    121     mOemNetId = SetupOemNetwork();
    122 }
    123 
    124 void DnsResponderClient::TearDown() {
    125     TearDownOemNetwork(mOemNetId);
    126 }
    127