33 {
35 return true;
36 }
37
38 struct addrinfo hints, *res = nullptr;
39 memset(&hints, 0, sizeof(hints));
40 hints.ai_family = AF_UNSPEC;
41 hints.ai_socktype = SOCK_STREAM;
42
43 auto t0 = std::chrono::steady_clock::now();
44 int err = getaddrinfo(host.c_str(), nullptr, &hints, &res);
45 auto t1 = std::chrono::steady_clock::now();
46 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count();
47
48 bool ok = false;
49 if (err == 0 && res) {
50 for (struct addrinfo *p = res; p != nullptr; p = p->ai_next) {
51 char addrbuf[INET6_ADDRSTRLEN] = {0};
53 struct sockaddr_in *sa = (struct sockaddr_in *)p->ai_addr;
54 if (inet_ntop(
AF_INET, &(sa->sin_addr), addrbuf,
sizeof(addrbuf)))
55 addrs.emplace_back(addrbuf);
56 }
else if (p->ai_family ==
AF_INET6) {
57 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)p->ai_addr;
58 if (inet_ntop(
AF_INET6, &(sa6->sin6_addr), addrbuf,
sizeof(addrbuf)))
59 addrs.emplace_back(addrbuf);
60 }
61 }
62 ok = !addrs.empty();
63 }
64 if (res) freeaddrinfo(res);
66 return ok;
67}
static DNSCache & instance()
void store(const std::string &host, const std::vector< std::string > &addrs, int ttl_seconds=-1)