dnscache.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #include "dnscache.h"
  2. #include "probes.h"
  3. #include "timekeeper.h"
  4. #include <contrib/libs/c-ares/ares.h>
  5. #include <util/system/guard.h>
  6. #include <util/datetime/systime.h>
  7. const TDnsCache::THost TDnsCache::NullHost;
  8. LWTRACE_USING(DNSCACHELIB_PROVIDER);
  9. static_assert(sizeof(ares_channel) == sizeof(void*), "expect sizeof(ares_channel) == sizeof(void *)");
  10. TDnsCache::TDnsCache(bool allowIpv4, bool allowIpv6, time_t lifetime, time_t neg, ui32 timeout)
  11. : EntryLifetime(lifetime)
  12. , NegativeLifetime(neg)
  13. , Timeout(TDuration::MicroSeconds(timeout))
  14. , AllowIpV4(allowIpv4)
  15. , AllowIpV6(allowIpv6)
  16. , ACacheHits(0)
  17. , ACacheMisses(0)
  18. , PtrCacheHits(0)
  19. , PtrCacheMisses(0)
  20. {
  21. #ifdef _win_
  22. if (ares_library_init(ARES_LIB_INIT_WIN32) != ARES_SUCCESS) {
  23. LWPROBE(AresInitFailed);
  24. ythrow yexception() << "ares_init() failed";
  25. }
  26. #endif
  27. ares_channel chan;
  28. if (ares_init(&chan) != ARES_SUCCESS) {
  29. LWPROBE(AresInitFailed);
  30. ythrow yexception() << "ares_init() failed";
  31. }
  32. Channel = chan;
  33. LWPROBE(Created);
  34. }
  35. TDnsCache::~TDnsCache(void) {
  36. ares_channel chan = static_cast<ares_channel>(Channel);
  37. ares_cancel(chan);
  38. ares_destroy(chan);
  39. LWPROBE(Destroyed);
  40. #ifdef _win_
  41. ares_library_cleanup();
  42. #endif
  43. }
  44. TString TDnsCache::GetHostByAddr(const NAddr::IRemoteAddr& addr) {
  45. in6_addr key;
  46. if (addr.Addr()->sa_family == AF_INET6) {
  47. const struct sockaddr_in6* s6 = (const struct sockaddr_in6*)(addr.Addr());
  48. memcpy(&key, &s6->sin6_addr, sizeof(s6->sin6_addr));
  49. } else if (addr.Addr()->sa_family == AF_INET) {
  50. const struct sockaddr_in* s4 = (const struct sockaddr_in*)(addr.Addr());
  51. memset(&key, 0, sizeof(key));
  52. memcpy(&key, &s4->sin_addr, sizeof(s4->sin_addr));
  53. } else {
  54. return "";
  55. }
  56. const TAddr& host = ResolveAddr(key, addr.Addr()->sa_family);
  57. return host.Hostname;
  58. }
  59. TIpHost TDnsCache::Get(const TString& hostname) {
  60. if (!AllowIpV4)
  61. return TIpHost(-1);
  62. const THost& addr = Resolve(hostname, AF_INET);
  63. TGuard<TMutex> lock(CacheMtx);
  64. if (addr.AddrsV4.empty()) {
  65. return TIpHost(-1);
  66. }
  67. return addr.AddrsV4.front();
  68. }
  69. NAddr::IRemoteAddrPtr TDnsCache::GetAddr(
  70. const TString& hostname,
  71. int family,
  72. TIpPort port,
  73. bool cacheOnly) {
  74. if (family != AF_INET && AllowIpV6) {
  75. const THost& addr = Resolve(hostname, AF_INET6, cacheOnly);
  76. TGuard<TMutex> lock(CacheMtx);
  77. if (!addr.AddrsV6.empty()) {
  78. struct sockaddr_in6 sin6;
  79. Zero(sin6);
  80. sin6.sin6_family = AF_INET6;
  81. sin6.sin6_addr = addr.AddrsV6.front();
  82. sin6.sin6_port = HostToInet(port);
  83. return MakeHolder<NAddr::TIPv6Addr>(sin6);
  84. }
  85. }
  86. if (family != AF_INET6 && AllowIpV4) {
  87. const THost& addr = Resolve(hostname, AF_INET, cacheOnly);
  88. TGuard<TMutex> lock(CacheMtx);
  89. if (!addr.AddrsV4.empty()) {
  90. return MakeHolder<NAddr::TIPv4Addr>(TIpAddress(addr.AddrsV4.front(), port));
  91. }
  92. }
  93. LWPROBE(FamilyMismatch, family, AllowIpV4, AllowIpV6);
  94. return nullptr;
  95. }
  96. void TDnsCache::GetAllAddresses(
  97. const TString& hostname,
  98. TVector<NAddr::IRemoteAddrPtr>& addrs) {
  99. if (AllowIpV4) {
  100. const THost& addr4 = Resolve(hostname, AF_INET);
  101. TGuard<TMutex> lock(CacheMtx);
  102. for (size_t i = 0; i < addr4.AddrsV4.size(); i++) {
  103. addrs.push_back(MakeHolder<NAddr::TIPv4Addr>(TIpAddress(addr4.AddrsV4[i], 0)));
  104. }
  105. }
  106. if (AllowIpV6) {
  107. const THost& addr6 = Resolve(hostname, AF_INET6);
  108. struct sockaddr_in6 sin6;
  109. Zero(sin6);
  110. sin6.sin6_family = AF_INET6;
  111. TGuard<TMutex> lock(CacheMtx);
  112. for (size_t i = 0; i < addr6.AddrsV6.size(); i++) {
  113. sin6.sin6_addr = addr6.AddrsV6[i];
  114. addrs.push_back(MakeHolder<NAddr::TIPv6Addr>(sin6));
  115. }
  116. }
  117. }
  118. void TDnsCache::GetStats(ui64& a_cache_hits, ui64& a_cache_misses,
  119. ui64& ptr_cache_hits, ui64& ptr_cache_misses) {
  120. TGuard<TMutex> lock(CacheMtx);
  121. a_cache_hits = ACacheHits;
  122. a_cache_misses = ACacheMisses;
  123. ptr_cache_hits = PtrCacheHits;
  124. ptr_cache_misses = PtrCacheMisses;
  125. }
  126. bool TDnsCache::THost::IsStale(int family, const TDnsCache* ctx) const noexcept {
  127. time_t resolved = family == AF_INET ? ResolvedV4 : ResolvedV6;
  128. time_t notfound = family == AF_INET ? NotFoundV4 : NotFoundV6;
  129. if (TTimeKeeper::GetTime() - resolved < ctx->EntryLifetime)
  130. return false;
  131. if (TTimeKeeper::GetTime() - notfound < ctx->NegativeLifetime)
  132. return false;
  133. return true;
  134. }
  135. const TDnsCache::THost&
  136. TDnsCache::Resolve(const TString& hostname, int family, bool cacheOnly) {
  137. if (!ValidateHName(hostname)) {
  138. LWPROBE(ResolveNullHost, hostname, family);
  139. return NullHost;
  140. }
  141. THostCache::iterator p;
  142. Y_ASSERT(family == AF_INET || family == AF_INET6);
  143. {
  144. TGuard<TMutex> lock(CacheMtx);
  145. p = HostCache.find(hostname);
  146. if (p != HostCache.end()) {
  147. if (!p->second.IsStale(family, this)) {
  148. /* Recently resolved, just return cached value */
  149. ACacheHits += 1;
  150. THost& host = p->second;
  151. LWPROBE(ResolveFromCache, hostname, family, host.AddrsV4ToString(), host.AddrsV6ToString(), ACacheHits);
  152. return host;
  153. } else {
  154. LWPROBE(ResolveCacheTimeout, hostname);
  155. }
  156. } else {
  157. /* Never resolved, create cache entry */
  158. LWPROBE(ResolveCacheNew, hostname);
  159. p = HostCache.insert(std::make_pair(hostname, THost())).first;
  160. }
  161. ACacheMisses += 1;
  162. }
  163. if (cacheOnly)
  164. return NullHost;
  165. TAtomic& inprogress = (family == AF_INET ? p->second.InProgressV4 : p->second.InProgressV6);
  166. {
  167. /* This way only! CacheMtx should always be taken AFTER AresMtx,
  168. * because later in ares_process it can only be done this way.
  169. * Lock order reversal will cause deadlock in unfortunate monents.
  170. */
  171. TGuard<TMutex> areslock(AresMtx);
  172. TGuard<TMutex> cachelock(CacheMtx);
  173. if (!inprogress) {
  174. ares_channel chan = static_cast<ares_channel>(Channel);
  175. TGHBNContext* ctx = new TGHBNContext();
  176. ctx->Owner = this;
  177. ctx->Hostname = hostname;
  178. ctx->Family = family;
  179. AtomicSet(inprogress, 1);
  180. ares_gethostbyname(chan, hostname.c_str(), family,
  181. &TDnsCache::GHBNCallback, ctx);
  182. }
  183. }
  184. WaitTask(inprogress);
  185. LWPROBE(ResolveDone, hostname, family, p->second.AddrsV4ToString(), p->second.AddrsV6ToString());
  186. return p->second;
  187. }
  188. bool TDnsCache::ValidateHName(const TString& name) const noexcept {
  189. return name.size() > 0;
  190. }
  191. const TDnsCache::TAddr& TDnsCache::ResolveAddr(const in6_addr& addr, int family) {
  192. TAddrCache::iterator p;
  193. {
  194. TGuard<TMutex> lock(CacheMtx);
  195. p = AddrCache.find(addr);
  196. if (p != AddrCache.end()) {
  197. if (TTimeKeeper::GetTime() - p->second.Resolved < EntryLifetime || TTimeKeeper::GetTime() - p->second.NotFound < NegativeLifetime) {
  198. /* Recently resolved, just return cached value */
  199. PtrCacheHits += 1;
  200. return p->second;
  201. }
  202. } else {
  203. /* Never resolved, create cache entry */
  204. p = AddrCache.insert(std::make_pair(addr, TAddr())).first;
  205. }
  206. PtrCacheMisses += 1;
  207. }
  208. {
  209. /* This way only! CacheMtx should always be taken AFTER AresMtx,
  210. * because later in ares_process it can only be done this way.
  211. * Lock order reversal will cause deadlock in unfortunate monents.
  212. */
  213. TGuard<TMutex> areslock(AresMtx);
  214. TGuard<TMutex> cachelock(CacheMtx);
  215. if (!p->second.InProgress) {
  216. ares_channel chan = static_cast<ares_channel>(Channel);
  217. TGHBAContext* ctx = new TGHBAContext();
  218. ctx->Owner = this;
  219. ctx->Addr = addr;
  220. AtomicSet(p->second.InProgress, 1);
  221. ares_gethostbyaddr(chan, &addr,
  222. family == AF_INET ? sizeof(in_addr) : sizeof(in6_addr),
  223. family, &TDnsCache::GHBACallback, ctx);
  224. }
  225. }
  226. WaitTask(p->second.InProgress);
  227. return p->second;
  228. }
  229. void TDnsCache::WaitTask(TAtomic& flag) {
  230. const TInstant start = TInstant(TTimeKeeper::GetTimeval());
  231. while (AtomicGet(flag)) {
  232. ares_channel chan = static_cast<ares_channel>(Channel);
  233. struct pollfd pfd[ARES_GETSOCK_MAXNUM];
  234. int nfds;
  235. ares_socket_t socks[ARES_GETSOCK_MAXNUM];
  236. int bits;
  237. {
  238. TGuard<TMutex> lock(AresMtx);
  239. bits = ares_getsock(chan, socks, ARES_GETSOCK_MAXNUM);
  240. if (bits == 0) {
  241. /* other thread did our job */
  242. continue;
  243. }
  244. }
  245. for (nfds = 0; nfds < ARES_GETSOCK_MAXNUM; nfds++) {
  246. pfd[nfds].events = 0;
  247. pfd[nfds].revents = 0;
  248. if (ARES_GETSOCK_READABLE(bits, nfds)) {
  249. pfd[nfds].fd = socks[nfds];
  250. pfd[nfds].events |= POLLRDNORM | POLLIN;
  251. }
  252. if (ARES_GETSOCK_WRITABLE(bits, nfds)) {
  253. pfd[nfds].fd = socks[nfds];
  254. pfd[nfds].events |= POLLWRNORM | POLLOUT;
  255. }
  256. if (pfd[nfds].events == 0) {
  257. break;
  258. }
  259. }
  260. Y_ASSERT(nfds != 0);
  261. const TDuration left = TInstant(TTimeKeeper::GetTimeval()) - start;
  262. const TDuration wait = Max(Timeout - left, TDuration::Zero());
  263. int rv = poll(pfd, nfds, wait.MilliSeconds());
  264. if (rv == -1) {
  265. if (errno == EINTR) {
  266. continue;
  267. }
  268. /* Unknown error in select, can't recover. Just pretend there was no reply */
  269. rv = 0;
  270. }
  271. if (rv == 0) {
  272. /* poll() timed out */
  273. TGuard<TMutex> lock(AresMtx);
  274. ares_process_fd(chan, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
  275. } else {
  276. for (int i = 0; i < nfds; i++) {
  277. if (pfd[i].revents == 0) {
  278. continue;
  279. }
  280. TGuard<TMutex> lock(AresMtx);
  281. ares_process_fd(chan,
  282. pfd[i].revents & (POLLRDNORM | POLLIN)
  283. ? pfd[i].fd
  284. : ARES_SOCKET_BAD,
  285. pfd[i].revents & (POLLWRNORM | POLLOUT)
  286. ? pfd[i].fd
  287. : ARES_SOCKET_BAD);
  288. }
  289. }
  290. if (start + Timeout <= TInstant(TTimeKeeper::GetTimeval())) {
  291. break;
  292. }
  293. }
  294. }
  295. void TDnsCache::GHBNCallback(void* arg, int status, int, struct hostent* info) {
  296. THolder<TGHBNContext> ctx(static_cast<TGHBNContext*>(arg));
  297. TGuard<TMutex> lock(ctx->Owner->CacheMtx);
  298. THostCache::iterator p = ctx->Owner->HostCache.find(ctx->Hostname);
  299. Y_ASSERT(p != ctx->Owner->HostCache.end());
  300. time_t& resolved = (ctx->Family == AF_INET ? p->second.ResolvedV4 : p->second.ResolvedV6);
  301. time_t& notfound = (ctx->Family == AF_INET ? p->second.NotFoundV4 : p->second.NotFoundV6);
  302. TAtomic& inprogress = (ctx->Family == AF_INET ? p->second.InProgressV4 : p->second.InProgressV6);
  303. if (status == ARES_SUCCESS) {
  304. if (info->h_addrtype == AF_INET) {
  305. p->second.AddrsV4.clear();
  306. for (int i = 0; info->h_addr_list[i] != nullptr; i++) {
  307. p->second.AddrsV4.push_back(*(TIpHost*)(info->h_addr_list[i]));
  308. }
  309. /* It is possible to ask ares for IPv6 and have IPv4 addrs instead,
  310. so take care and set V4 timers anyway.
  311. */
  312. p->second.ResolvedV4 = TTimeKeeper::GetTime();
  313. p->second.ResolvedV4 = 0;
  314. AtomicSet(p->second.InProgressV4, 0);
  315. } else if (info->h_addrtype == AF_INET6) {
  316. p->second.AddrsV6.clear();
  317. for (int i = 0; info->h_addr_list[i] != nullptr; i++) {
  318. p->second.AddrsV6.push_back(*(struct in6_addr*)(info->h_addr_list[i]));
  319. }
  320. } else {
  321. Y_FAIL("unknown address type in ares callback");
  322. }
  323. resolved = TTimeKeeper::GetTime();
  324. notfound = 0;
  325. } else {
  326. notfound = TTimeKeeper::GetTime();
  327. resolved = 0;
  328. }
  329. AtomicSet(inprogress, 0);
  330. }
  331. void TDnsCache::GHBACallback(void* arg, int status, int, struct hostent* info) {
  332. THolder<TGHBAContext> ctx(static_cast<TGHBAContext*>(arg));
  333. TGuard<TMutex> lock(ctx->Owner->CacheMtx);
  334. TAddrCache::iterator p = ctx->Owner->AddrCache.find(ctx->Addr);
  335. Y_ASSERT(p != ctx->Owner->AddrCache.end());
  336. if (status == ARES_SUCCESS) {
  337. p->second.Hostname = info->h_name;
  338. p->second.Resolved = TTimeKeeper::GetTime();
  339. p->second.NotFound = 0;
  340. } else {
  341. p->second.NotFound = TTimeKeeper::GetTime();
  342. p->second.Resolved = 0;
  343. }
  344. AtomicSet(p->second.InProgress, 0);
  345. }
  346. TString TDnsCache::THost::AddrsV4ToString() const {
  347. TStringStream ss;
  348. bool first = false;
  349. for (TIpHost addr : AddrsV4) {
  350. ss << (first ? "" : " ") << IpToString(addr);
  351. first = false;
  352. }
  353. return ss.Str();
  354. }
  355. TString TDnsCache::THost::AddrsV6ToString() const {
  356. TStringStream ss;
  357. bool first = false;
  358. for (in6_addr addr : AddrsV6) {
  359. struct sockaddr_in6 sin6;
  360. Zero(sin6);
  361. sin6.sin6_family = AF_INET6;
  362. sin6.sin6_addr = addr;
  363. NAddr::TIPv6Addr addr6(sin6);
  364. ss << (first ? "" : " ") << NAddr::PrintHost(addr6);
  365. first = false;
  366. }
  367. return ss.Str();
  368. }
  369. TDnsCache::TAresLibInit TDnsCache::InitAresLib;