DNSCache.cc 5.4 KB

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