DNSCache.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 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. #ifndef _D_DNS_CACHE_H_
  36. #define _D_DNS_CACHE_H_
  37. #include "common.h"
  38. #include <string>
  39. #include <deque>
  40. #include <algorithm>
  41. #include <vector>
  42. #include "A2STR.h"
  43. namespace aria2 {
  44. class DNSCache {
  45. private:
  46. struct AddrEntry {
  47. std::string addr_;
  48. bool good_;
  49. AddrEntry(const std::string& addr):addr_(addr), good_(true) {}
  50. };
  51. struct CacheEntry {
  52. std::string hostname_;
  53. uint16_t port_;
  54. std::vector<AddrEntry> addrEntries_;
  55. CacheEntry
  56. (const std::string& hostname, uint16_t port):
  57. hostname_(hostname), port_(port) {}
  58. void add(const std::string& addr)
  59. {
  60. addrEntries_.push_back(AddrEntry(addr));
  61. }
  62. std::vector<AddrEntry>::iterator find(const std::string& addr)
  63. {
  64. for(std::vector<AddrEntry>::iterator i = addrEntries_.begin(),
  65. eoi = addrEntries_.end(); i != eoi; ++i) {
  66. if((*i).addr_ == addr) {
  67. return i;
  68. }
  69. }
  70. return addrEntries_.end();
  71. }
  72. std::vector<AddrEntry>::const_iterator find(const std::string& addr) const
  73. {
  74. for(std::vector<AddrEntry>::const_iterator i = addrEntries_.begin(),
  75. eoi = addrEntries_.end(); i != eoi; ++i) {
  76. if((*i).addr_ == addr) {
  77. return i;
  78. }
  79. }
  80. return addrEntries_.end();
  81. }
  82. bool contains(const std::string& addr) const
  83. {
  84. return find(addr) != addrEntries_.end();
  85. }
  86. const std::string& getGoodAddr() const
  87. {
  88. for(std::vector<AddrEntry>::const_iterator i = addrEntries_.begin(),
  89. eoi = addrEntries_.end(); i != eoi; ++i) {
  90. if((*i).good_) {
  91. return (*i).addr_;
  92. }
  93. }
  94. return A2STR::NIL;
  95. }
  96. template<typename OutputIterator>
  97. void getAllGoodAddrs(OutputIterator out) const
  98. {
  99. for(std::vector<AddrEntry>::const_iterator i = addrEntries_.begin(),
  100. eoi = addrEntries_.end(); i != eoi; ++i) {
  101. if((*i).good_) {
  102. *out++ = (*i).addr_;
  103. }
  104. }
  105. }
  106. void markBad(const std::string& addr)
  107. {
  108. std::vector<AddrEntry>::iterator i = find(addr);
  109. if(i != addrEntries_.end()) {
  110. (*i).good_ = false;
  111. }
  112. }
  113. bool operator<(const CacheEntry& e) const
  114. {
  115. int r = hostname_.compare(e.hostname_);
  116. if(r != 0) {
  117. return r < 0;
  118. }
  119. return port_ < e.port_;
  120. }
  121. bool operator==(const CacheEntry& e) const
  122. {
  123. return hostname_ == e.hostname_ && port_ == e.port_;
  124. }
  125. };
  126. std::deque<CacheEntry> entries_;
  127. public:
  128. const std::string& find(const std::string& hostname, uint16_t port) const
  129. {
  130. CacheEntry target(hostname, port);
  131. std::deque<CacheEntry>::const_iterator i =
  132. std::lower_bound(entries_.begin(), entries_.end(), target);
  133. if(i != entries_.end() && (*i) == target) {
  134. return (*i).getGoodAddr();
  135. }
  136. return A2STR::NIL;
  137. }
  138. template<typename OutputIterator>
  139. void findAll
  140. (OutputIterator out, const std::string& hostname, uint16_t port) const
  141. {
  142. CacheEntry target(hostname, port);
  143. std::deque<CacheEntry>::const_iterator i =
  144. std::lower_bound(entries_.begin(), entries_.end(), target);
  145. if(i != entries_.end() && (*i) == target) {
  146. (*i).getAllGoodAddrs(out);
  147. }
  148. }
  149. void 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 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 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. };
  184. } // namespace aria2
  185. #endif // _D_DNS_CACHE_H_