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 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 18 #include "resolv_test_utils.h" 19 20 #include <arpa/inet.h> 21 22 #include <android-base/chrono_utils.h> 23 #include <netdutils/InternetAddresses.h> 24 25 using android::netdutils::ScopedAddrinfo; 26 27 std::string ToString(const hostent* he) { 28 if (he == nullptr) return "<null>"; 29 char buffer[INET6_ADDRSTRLEN]; 30 if (!inet_ntop(he->h_addrtype, he->h_addr_list[0], buffer, sizeof(buffer))) { 31 return "<invalid>"; 32 } 33 return buffer; 34 } 35 36 std::string ToString(const addrinfo* ai) { 37 if (!ai) return "<null>"; 38 39 char host[NI_MAXHOST]; 40 int rv = getnameinfo(ai->ai_addr, ai->ai_addrlen, host, sizeof(host), nullptr, 0, 41 NI_NUMERICHOST); 42 if (rv != 0) return gai_strerror(rv); 43 return host; 44 } 45 46 std::string ToString(const ScopedAddrinfo& ai) { 47 return ToString(ai.get()); 48 } 49 50 std::string ToString(const sockaddr_storage* addr) { 51 if (!addr) return "<null>"; 52 char host[NI_MAXHOST]; 53 int rv = getnameinfo((const sockaddr*)addr, sizeof(sockaddr_storage), host, sizeof(host), 54 nullptr, 0, NI_NUMERICHOST); 55 if (rv != 0) return gai_strerror(rv); 56 return host; 57 } 58 59 std::vector<std::string> ToStrings(const hostent* he) { 60 std::vector<std::string> hosts; 61 if (he == nullptr) { 62 hosts.push_back("<null>"); 63 return hosts; 64 } 65 uint32_t i = 0; 66 while (he->h_addr_list[i] != nullptr) { 67 char host[INET6_ADDRSTRLEN]; 68 if (!inet_ntop(he->h_addrtype, he->h_addr_list[i], host, sizeof(host))) { 69 hosts.push_back("<invalid>"); 70 return hosts; 71 } else { 72 hosts.push_back(host); 73 } 74 i++; 75 } 76 if (hosts.empty()) hosts.push_back("<invalid>"); 77 return hosts; 78 } 79 80 std::vector<std::string> ToStrings(const addrinfo* ai) { 81 std::vector<std::string> hosts; 82 if (!ai) { 83 hosts.push_back("<null>"); 84 return hosts; 85 } 86 for (const auto* aip = ai; aip != nullptr; aip = aip->ai_next) { 87 char host[NI_MAXHOST]; 88 int rv = getnameinfo(aip->ai_addr, aip->ai_addrlen, host, sizeof(host), nullptr, 0, 89 NI_NUMERICHOST); 90 if (rv != 0) { 91 hosts.clear(); 92 hosts.push_back(gai_strerror(rv)); 93 return hosts; 94 } else { 95 hosts.push_back(host); 96 } 97 } 98 if (hosts.empty()) hosts.push_back("<invalid>"); 99 return hosts; 100 } 101 102 std::vector<std::string> ToStrings(const ScopedAddrinfo& ai) { 103 return ToStrings(ai.get()); 104 } 105 106 size_t GetNumQueries(const test::DNSResponder& dns, const char* name) { 107 std::vector<test::DNSResponder::QueryInfo> queries = dns.queries(); 108 size_t found = 0; 109 for (const auto& p : queries) { 110 if (p.name == name) { 111 ++found; 112 } 113 } 114 return found; 115 } 116 117 size_t GetNumQueriesForProtocol(const test::DNSResponder& dns, const int protocol, 118 const char* name) { 119 std::vector<test::DNSResponder::QueryInfo> queries = dns.queries(); 120 size_t found = 0; 121 for (const auto& p : queries) { 122 if (p.protocol == protocol && p.name == name) { 123 ++found; 124 } 125 } 126 return found; 127 } 128 129 size_t GetNumQueriesForType(const test::DNSResponder& dns, ns_type type, const char* name) { 130 std::vector<test::DNSResponder::QueryInfo> queries = dns.queries(); 131 size_t found = 0; 132 for (const auto& p : queries) { 133 if (p.type == type && p.name == name) { 134 ++found; 135 } 136 } 137 return found; 138 } 139 140 bool PollForCondition(const std::function<bool()>& condition, std::chrono::milliseconds timeout) { 141 constexpr std::chrono::milliseconds retryIntervalMs{5}; 142 android::base::Timer t; 143 while (t.duration() < timeout) { 144 if (condition()) return true; 145 std::this_thread::sleep_for(retryIntervalMs); 146 } 147 return false; 148 } 149