DNSCache.cc 5.6 KB

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