1 /* 2 * Copyright (C) 2019 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 requied 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 18 #ifndef DNS_RESPONDER_CLIENT_H 19 #define DNS_RESPONDER_CLIENT_H 20 21 #include <memory> 22 #include <string> 23 #include <vector> 24 25 #include <cutils/sockets.h> 26 27 #include <private/android_filesystem_config.h> 28 #include <utils/StrongPointer.h> 29 30 #include "NetdClient.h" 31 #include "android/net/IDnsResolver.h" 32 #include "android/net/INetd.h" 33 #include "binder/IServiceManager.h" 34 #include "dns_responder.h" 35 36 inline const std::vector<std::string> kDefaultServers = {"127.0.0.3"}; 37 inline const std::vector<std::string> kDefaultSearchDomains = {"example.com"}; 38 inline const std::vector<int> kDefaultParams = { 39 300, // sample validity in seconds 40 25, // success threshod in percent 41 8, 8, // {MIN,MAX}_SAMPLES 42 1000, // BASE_TIMEOUT_MSEC 43 2, // retry count 44 }; 45 46 class DnsResponderClient { 47 public: 48 struct Mapping { 49 std::string host; 50 std::string entry; 51 std::string ip4; 52 std::string ip6; 53 }; 54 55 virtual ~DnsResponderClient() = default; 56 57 static void SetupMappings(unsigned num_hosts, const std::vector<std::string>& domains, 58 std::vector<Mapping>* mappings); 59 60 bool SetResolversForNetwork(const std::vector<std::string>& servers = kDefaultServers, 61 const std::vector<std::string>& domains = kDefaultSearchDomains, 62 const std::vector<int>& params = kDefaultParams); 63 64 bool SetResolversForNetwork(const std::vector<std::string>& servers, 65 const std::vector<std::string>& searchDomains, 66 const std::string& params); 67 SetResolversWithTls(const std::vector<std::string> & servers,const std::vector<std::string> & searchDomains,const std::vector<int> & params,const std::string & name,const std::vector<std::string> & fingerprints)68 bool SetResolversWithTls(const std::vector<std::string>& servers, 69 const std::vector<std::string>& searchDomains, 70 const std::vector<int>& params, 71 const std::string& name, 72 const std::vector<std::string>& fingerprints) { 73 // Pass servers as both network-assigned and TLS servers. Tests can 74 // determine on which server and by which protocol queries arrived. 75 return SetResolversWithTls(servers, searchDomains, params, 76 servers, name, fingerprints); 77 } 78 79 bool SetResolversWithTls(const std::vector<std::string>& servers, 80 const std::vector<std::string>& searchDomains, 81 const std::vector<int>& params, 82 const std::vector<std::string>& tlsServers, 83 const std::string& name, 84 const std::vector<std::string>& fingerprints); 85 86 static void SetupDNSServers(unsigned num_servers, const std::vector<Mapping>& mappings, 87 std::vector<std::unique_ptr<test::DNSResponder>>* dns, 88 std::vector<std::string>* servers); 89 90 int SetupOemNetwork(); 91 92 void TearDownOemNetwork(int oemNetId); 93 94 virtual void SetUp(); 95 virtual void TearDown(); 96 resolvService()97 android::net::IDnsResolver* resolvService() const { return mDnsResolvSrv.get(); } netdService()98 android::net::INetd* netdService() const { return mNetdSrv.get(); } 99 100 private: 101 android::sp<android::net::INetd> mNetdSrv = nullptr; 102 android::sp<android::net::IDnsResolver> mDnsResolvSrv = nullptr; 103 int mOemNetId = -1; 104 }; 105 106 #endif // DNS_RESPONDER_CLIENT_H 107