DNSCache.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. DNSCache::AddrEntry::AddrEntry(const AddrEntry& c)
  42. : addr_(c.addr_), good_(c.good_)
  43. {}
  44. DNSCache::AddrEntry::~AddrEntry() {}
  45. DNSCache::AddrEntry& DNSCache::AddrEntry::operator=(const AddrEntry& c)
  46. {
  47. if(this != &c) {
  48. addr_ = c.addr_;
  49. good_ = c.good_;
  50. }
  51. return *this;
  52. }
  53. DNSCache::CacheEntry::CacheEntry(const std::string& hostname, uint16_t port)
  54. : hostname_(hostname), port_(port)
  55. {}
  56. DNSCache::CacheEntry::CacheEntry(const CacheEntry& c)
  57. : hostname_(c.hostname_), port_(c.port_), addrEntries_(c.addrEntries_)
  58. {}
  59. DNSCache::CacheEntry::~CacheEntry() {}
  60. DNSCache::CacheEntry& DNSCache::CacheEntry::operator=(const CacheEntry& c)
  61. {
  62. if(this != &c) {
  63. hostname_ = c.hostname_;
  64. port_ = c.port_;
  65. addrEntries_ = c.addrEntries_;
  66. }
  67. return *this;
  68. }
  69. bool DNSCache::CacheEntry::add(const std::string& addr)
  70. {
  71. for(std::vector<AddrEntry>::const_iterator i = addrEntries_.begin(),
  72. eoi = addrEntries_.end(); i != eoi; ++i) {
  73. if((*i).addr_ == addr) {
  74. return false;
  75. }
  76. }
  77. addrEntries_.push_back(AddrEntry(addr));
  78. return true;
  79. }
  80. std::vector<DNSCache::AddrEntry>::iterator DNSCache::CacheEntry::find
  81. (const std::string& addr)
  82. {
  83. for(auto i = addrEntries_.begin(), eoi = addrEntries_.end(); i != eoi; ++i) {
  84. if((*i).addr_ == addr) {
  85. return i;
  86. }
  87. }
  88. return addrEntries_.end();
  89. }
  90. std::vector<DNSCache::AddrEntry>::const_iterator DNSCache::CacheEntry::find
  91. (const std::string& addr) const
  92. {
  93. for(auto i = addrEntries_.begin(), eoi = addrEntries_.end(); i != eoi; ++i) {
  94. if((*i).addr_ == addr) {
  95. return i;
  96. }
  97. }
  98. return addrEntries_.end();
  99. }
  100. bool DNSCache::CacheEntry::contains(const std::string& addr) const
  101. {
  102. return find(addr) != addrEntries_.end();
  103. }
  104. const std::string& DNSCache::CacheEntry::getGoodAddr() const
  105. {
  106. for(auto & elem : addrEntries_) {
  107. if(elem.good_) {
  108. return (elem).addr_;
  109. }
  110. }
  111. return A2STR::NIL;
  112. }
  113. void DNSCache::CacheEntry::markBad(const std::string& addr)
  114. {
  115. auto i = find(addr);
  116. if(i != addrEntries_.end()) {
  117. i->good_ = false;
  118. }
  119. }
  120. bool DNSCache::CacheEntry::operator<(const CacheEntry& e) const
  121. {
  122. int r = hostname_.compare(e.hostname_);
  123. if(r != 0) {
  124. return r < 0;
  125. }
  126. return port_ < e.port_;
  127. }
  128. bool DNSCache::CacheEntry::operator==(const CacheEntry& e) const
  129. {
  130. return hostname_ == e.hostname_ && port_ == e.port_;
  131. }
  132. DNSCache::DNSCache() {}
  133. DNSCache::DNSCache(const DNSCache& c):entries_(c.entries_) {}
  134. DNSCache::~DNSCache() {}
  135. DNSCache& DNSCache::operator=(const DNSCache& c)
  136. {
  137. if(this != &c) {
  138. entries_ = c.entries_;
  139. }
  140. return *this;
  141. }
  142. const std::string& DNSCache::find
  143. (const std::string& hostname, uint16_t port) const
  144. {
  145. std::shared_ptr<CacheEntry> target(new CacheEntry(hostname, port));
  146. CacheEntrySet::iterator i = entries_.find(target);
  147. if(i == entries_.end()) {
  148. return A2STR::NIL;
  149. } else {
  150. return (*i)->getGoodAddr();
  151. }
  152. }
  153. void DNSCache::put
  154. (const std::string& hostname, const std::string& ipaddr, uint16_t port)
  155. {
  156. std::shared_ptr<CacheEntry> target(new CacheEntry(hostname, port));
  157. CacheEntrySet::iterator i = entries_.lower_bound(target);
  158. if(i != entries_.end() && *(*i) == *target) {
  159. (*i)->add(ipaddr);
  160. } else {
  161. target->add(ipaddr);
  162. entries_.insert(i, target);
  163. }
  164. }
  165. void DNSCache::markBad
  166. (const std::string& hostname, const std::string& ipaddr, uint16_t port)
  167. {
  168. std::shared_ptr<CacheEntry> target(new CacheEntry(hostname, port));
  169. CacheEntrySet::iterator i = entries_.find(target);
  170. if(i != entries_.end()) {
  171. (*i)->markBad(ipaddr);
  172. }
  173. }
  174. void DNSCache::remove(const std::string& hostname, uint16_t port)
  175. {
  176. std::shared_ptr<CacheEntry> target(new CacheEntry(hostname, port));
  177. entries_.erase(target);
  178. }
  179. } // namespace aria2