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 using android::net::INetd;
32 using android::net::ResolverParamsParcel;
33
SetupMappings(unsigned num_hosts,const std::vector<std::string> & domains,std::vector<Mapping> * mappings)34 void DnsResponderClient::SetupMappings(unsigned num_hosts, const std::vector<std::string>& domains,
35 std::vector<Mapping>* mappings) {
36 mappings->resize(num_hosts * domains.size());
37 auto mappings_it = mappings->begin();
38 for (unsigned i = 0 ; i < num_hosts ; ++i) {
39 for (const auto& domain : domains) {
40 mappings_it->host = StringPrintf("host%u", i);
41 mappings_it->entry = StringPrintf("%s.%s.", mappings_it->host.c_str(),
42 domain.c_str());
43 mappings_it->ip4 = StringPrintf("192.0.2.%u", i%253 + 1);
44 mappings_it->ip6 = StringPrintf("2001:db8::%x", i%65534 + 1);
45 ++mappings_it;
46 }
47 }
48 }
49
50 // TODO: Use SetResolverConfiguration() with ResolverParamsParcel struct directly.
51 // DEPRECATED: Use SetResolverConfiguration() in new code
makeResolverParamsParcel(int netId,const std::vector<int> & params,const std::vector<std::string> & servers,const std::vector<std::string> & domains,const std::string & tlsHostname,const std::vector<std::string> & tlsServers,const std::vector<std::string> & tlsFingerprints)52 static ResolverParamsParcel makeResolverParamsParcel(
53 int netId, const std::vector<int>& params, const std::vector<std::string>& servers,
54 const std::vector<std::string>& domains, const std::string& tlsHostname,
55 const std::vector<std::string>& tlsServers,
56 const std::vector<std::string>& tlsFingerprints) {
57 using android::net::IDnsResolver;
58 ResolverParamsParcel paramsParcel;
59
60 paramsParcel.netId = netId;
61 paramsParcel.sampleValiditySeconds = params[IDnsResolver::RESOLVER_PARAMS_SAMPLE_VALIDITY];
62 paramsParcel.successThreshold = params[IDnsResolver::RESOLVER_PARAMS_SUCCESS_THRESHOLD];
63 paramsParcel.minSamples = params[IDnsResolver::RESOLVER_PARAMS_MIN_SAMPLES];
64 paramsParcel.maxSamples = params[IDnsResolver::RESOLVER_PARAMS_MAX_SAMPLES];
65 if (params.size() > IDnsResolver::RESOLVER_PARAMS_BASE_TIMEOUT_MSEC) {
66 paramsParcel.baseTimeoutMsec = params[IDnsResolver::RESOLVER_PARAMS_BASE_TIMEOUT_MSEC];
67 } else {
68 paramsParcel.baseTimeoutMsec = 0;
69 }
70 if (params.size() > IDnsResolver::RESOLVER_PARAMS_RETRY_COUNT) {
71 paramsParcel.retryCount = params[IDnsResolver::RESOLVER_PARAMS_RETRY_COUNT];
72 } else {
73 paramsParcel.retryCount = 0;
74 }
75 paramsParcel.servers = servers;
76 paramsParcel.domains = domains;
77 paramsParcel.tlsName = tlsHostname;
78 paramsParcel.tlsServers = tlsServers;
79 paramsParcel.tlsFingerprints = tlsFingerprints;
80
81 return paramsParcel;
82 }
83
SetResolversForNetwork(const std::vector<std::string> & servers,const std::vector<std::string> & domains,const std::vector<int> & params)84 bool DnsResponderClient::SetResolversForNetwork(const std::vector<std::string>& servers,
85 const std::vector<std::string>& domains, const std::vector<int>& params) {
86 const auto& resolverParams =
87 makeResolverParamsParcel(TEST_NETID, params, servers, domains, "", {}, {});
88 const auto rv = mDnsResolvSrv->setResolverConfiguration(resolverParams);
89 return rv.isOk();
90 }
91
SetResolversWithTls(const std::vector<std::string> & servers,const std::vector<std::string> & domains,const std::vector<int> & params,const std::vector<std::string> & tlsServers,const std::string & name,const std::vector<std::string> & fingerprints)92 bool DnsResponderClient::SetResolversWithTls(const std::vector<std::string>& servers,
93 const std::vector<std::string>& domains, const std::vector<int>& params,
94 const std::vector<std::string>& tlsServers,
95 const std::string& name, const std::vector<std::string>& fingerprints) {
96 const auto& resolverParams = makeResolverParamsParcel(TEST_NETID, params, servers, domains,
97 name, tlsServers, fingerprints);
98 const auto rv = mDnsResolvSrv->setResolverConfiguration(resolverParams);
99 if (!rv.isOk()) ALOGI("SetResolversWithTls() -> %s", rv.toString8().c_str());
100 return rv.isOk();
101 }
102
SetupDNSServers(unsigned num_servers,const std::vector<Mapping> & mappings,std::vector<std::unique_ptr<test::DNSResponder>> * dns,std::vector<std::string> * servers)103 void DnsResponderClient::SetupDNSServers(unsigned num_servers, const std::vector<Mapping>& mappings,
104 std::vector<std::unique_ptr<test::DNSResponder>>* dns,
105 std::vector<std::string>* servers) {
106 const char* listen_srv = "53";
107 dns->resize(num_servers);
108 servers->resize(num_servers);
109 for (unsigned i = 0 ; i < num_servers ; ++i) {
110 auto& server = (*servers)[i];
111 auto& d = (*dns)[i];
112 server = StringPrintf("127.0.0.%u", i + 100);
113 d = std::make_unique<test::DNSResponder>(server, listen_srv, 250, ns_rcode::ns_r_servfail);
114 for (const auto& mapping : mappings) {
115 d->addMapping(mapping.entry.c_str(), ns_type::ns_t_a, mapping.ip4.c_str());
116 d->addMapping(mapping.entry.c_str(), ns_type::ns_t_aaaa, mapping.ip6.c_str());
117 }
118 d->startServer();
119 }
120 }
121
SetupOemNetwork()122 int DnsResponderClient::SetupOemNetwork() {
123 mNetdSrv->networkDestroy(TEST_NETID);
124 mDnsResolvSrv->destroyNetworkCache(TEST_NETID);
125 auto ret = mNetdSrv->networkCreatePhysical(TEST_NETID, INetd::PERMISSION_NONE);
126 if (!ret.isOk()) {
127 fprintf(stderr, "Creating physical network %d failed, %s\n", TEST_NETID,
128 ret.toString8().string());
129 return -1;
130 }
131 ret = mDnsResolvSrv->createNetworkCache(TEST_NETID);
132 if (!ret.isOk()) {
133 fprintf(stderr, "Creating network cache %d failed, %s\n", TEST_NETID,
134 ret.toString8().string());
135 return -1;
136 }
137 setNetworkForProcess(TEST_NETID);
138 if ((unsigned)TEST_NETID != getNetworkForProcess()) {
139 return -1;
140 }
141 return TEST_NETID;
142 }
143
TearDownOemNetwork(int oemNetId)144 void DnsResponderClient::TearDownOemNetwork(int oemNetId) {
145 if (oemNetId != -1) {
146 mNetdSrv->networkDestroy(oemNetId);
147 mDnsResolvSrv->destroyNetworkCache(oemNetId);
148 }
149 }
150
SetUp()151 void DnsResponderClient::SetUp() {
152 // binder setup
153 auto binder = android::defaultServiceManager()->getService(android::String16("netd"));
154 mNetdSrv = android::interface_cast<android::net::INetd>(binder);
155
156 auto resolvBinder =
157 android::defaultServiceManager()->getService(android::String16("dnsresolver"));
158 mDnsResolvSrv = android::interface_cast<android::net::IDnsResolver>(resolvBinder);
159
160 // Ensure resolutions go via proxy.
161 setenv(ANDROID_DNS_MODE, "", 1);
162 mOemNetId = SetupOemNetwork();
163 }
164
TearDown()165 void DnsResponderClient::TearDown() {
166 TearDownOemNetwork(mOemNetId);
167 }
168