DNSCache.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "DNSCache.h"
  36. #include "A2STR.h"
  37. namespace aria2 {
  38. DNSCache::AddrEntry::AddrEntry(const std::string& addr)
  39. : addr_(addr), good_(true)
  40. {
  41. }
  42. DNSCache::AddrEntry::AddrEntry(const AddrEntry& c) = default;
  43. DNSCache::AddrEntry::~AddrEntry() = default;
  44. DNSCache::AddrEntry& DNSCache::AddrEntry::operator=(const AddrEntry& c)
  45. {
  46. if (this != &c) {
  47. addr_ = c.addr_;
  48. good_ = c.good_;
  49. }
  50. return *this;
  51. }
  52. DNSCache::CacheEntry::CacheEntry(const std::string& hostname, uint16_t port)
  53. : hostname_(hostname), port_(port)
  54. {
  55. }
  56. DNSCache::CacheEntry::CacheEntry(const CacheEntry& c) = default;
  57. DNSCache::CacheEntry::~CacheEntry() = default;
  58. DNSCache::CacheEntry& DNSCache::CacheEntry::operator=(const CacheEntry& c)
  59. {
  60. if (this != &c) {
  61. hostname_ = c.hostname_;
  62. port_ = c.port_;
  63. addrEntries_ = c.addrEntries_;
  64. }
  65. return *this;
  66. }
  67. bool DNSCache::CacheEntry::add(const std::string& addr)
  68. {
  69. for (std::vector<AddrEntry>::const_iterator i = addrEntries_.begin(),
  70. eoi = addrEntries_.end();
  71. i != eoi; ++i) {
  72. if ((*i).addr_ == addr) {
  73. return false;
  74. }
  75. }
  76. addrEntries_.push_back(AddrEntry(addr));
  77. return true;
  78. }
  79. std::vector<DNSCache::AddrEntry>::iterator
  80. DNSCache::CacheEntry::find(const std::string& addr)
  81. {
  82. for (auto i = addrEntries_.begin(), eoi = addrEntries_.end(); i != eoi; ++i) {
  83. if ((*i).addr_ == addr) {
  84. return i;
  85. }
  86. }
  87. return addrEntries_.end();
  88. }
  89. std::vector<DNSCache::AddrEntry>::const_iterator
  90. DNSCache::CacheEntry::find(const std::string& addr) const
  91. {
  92. for (auto i = addrEntries_.begin(), eoi = addrEntries_.end(); i != eoi; ++i) {
  93. if ((*i).addr_ == addr) {
  94. return i;
  95. }
  96. }
  97. return addrEntries_.end();
  98. }
  99. bool DNSCache::CacheEntry::contains(const std::string& addr) const
  100. {
  101. return find(addr) != addrEntries_.end();
  102. }
  103. const std::string& DNSCache::CacheEntry::getGoodAddr() const
  104. {
  105. for (auto& elem : addrEntries_) {
  106. if (elem.good_) {
  107. return (elem).addr_;
  108. }
  109. }
  110. return A2STR::NIL;
  111. }
  112. void DNSCache::CacheEntry::markBad(const std::string& addr)
  113. {
  114. auto i = find(addr);
  115. if (i != addrEntries_.end()) {
  116. i->good_ = false;
  117. }
  118. }
  119. bool DNSCache::CacheEntry::operator<(const CacheEntry& e) const
  120. {
  121. int r = hostname_.compare(e.hostname_);
  122. if (r != 0) {
  123. return r < 0;
  124. }
  125. return port_ < e.port_;
  126. }
  127. bool DNSCache::CacheEntry::operator==(const CacheEntry& e) const
  128. {
  129. return hostname_ == e.hostname_ && port_ == e.port_;
  130. }
  131. DNSCache::DNSCache() {}
  132. DNSCache::DNSCache(const DNSCache& c) = default;
  133. DNSCache::~DNSCache() = default;
  134. DNSCache& DNSCache::operator=(const DNSCache& c)
  135. {
  136. if (this != &c) {
  137. entries_ = c.entries_;
  138. }
  139. return *this;
  140. }
  141. const std::string& DNSCache::find(const std::string& hostname,
  142. uint16_t port) const
  143. {
  144. auto target = std::make_shared<CacheEntry>(hostname, port);
  145. auto i = entries_.find(target);
  146. if (i == entries_.end()) {
  147. return A2STR::NIL;
  148. }
  149. else {
  150. return (*i)->getGoodAddr();
  151. }
  152. }
  153. void DNSCache::put(const std::string& hostname, const std::string& ipaddr,
  154. uint16_t port)
  155. {
  156. auto target = std::make_shared<CacheEntry>(hostname, port);
  157. auto i = entries_.lower_bound(target);
  158. if (i != entries_.end() && *(*i) == *target) {
  159. (*i)->add(ipaddr);
  160. }
  161. else {
  162. target->add(ipaddr);
  163. entries_.insert(i, target);
  164. }
  165. }
  166. void DNSCache::markBad(const std::string& hostname, const std::string& ipaddr,
  167. uint16_t port)
  168. {
  169. auto target = std::make_shared<CacheEntry>(hostname, port);
  170. auto i = entries_.find(target);
  171. if (i != entries_.end()) {
  172. (*i)->markBad(ipaddr);
  173. }
  174. }
  175. void DNSCache::remove(const std::string& hostname, uint16_t port)
  176. {
  177. auto target = std::make_shared<CacheEntry>(hostname, port);
  178. entries_.erase(target);
  179. }
  180. } // namespace aria2