rrparamregistry-placeholder.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <cassert>
  15. #include <algorithm>
  16. #include <cctype>
  17. #include <functional>
  18. #include <map>
  19. #include <string>
  20. #include <sstream>
  21. #include <utility>
  22. #include <stdint.h>
  23. #include <boost/shared_ptr.hpp>
  24. #include <exceptions/exceptions.h>
  25. #include <dns/rrparamregistry.h>
  26. #include <dns/rrclass.h>
  27. #include <dns/rrtype.h>
  28. #include <dns/rdata.h>
  29. #include <dns/rdataclass.h>
  30. using namespace std;
  31. using namespace boost;
  32. using namespace isc::util;
  33. using namespace isc::dns::rdata;
  34. namespace isc {
  35. namespace dns {
  36. namespace {
  37. ///
  38. /// The following function and class are a helper to define case-insensitive
  39. /// equivalence relationship on strings. They are used in the mapping
  40. /// containers below.
  41. ///
  42. bool
  43. CICharLess(char c1, char c2) {
  44. return (tolower(static_cast<unsigned char>(c1)) <
  45. tolower(static_cast<unsigned char>(c2)));
  46. }
  47. struct CIStringLess :
  48. public binary_function<string, string, bool>
  49. {
  50. bool operator()(const string& s1, const string& s2) const
  51. {
  52. return (lexicographical_compare(s1.begin(), s1.end(),
  53. s2.begin(), s2.end(), CICharLess));
  54. }
  55. };
  56. struct RRTypeParam {
  57. RRTypeParam(const string& code_string, uint16_t code) :
  58. code_string_(code_string), code_(code) {}
  59. string code_string_;
  60. uint16_t code_;
  61. /// magic constants
  62. static const unsigned int MAX_CODE = 0xffff;
  63. static const string& UNKNOWN_PREFIX();
  64. static size_t UNKNOWN_PREFIXLEN();
  65. static const string& UNKNOWN_MAX();
  66. static size_t UNKNOWN_MAXLEN();
  67. };
  68. typedef boost::shared_ptr<RRTypeParam> RRTypeParamPtr;
  69. typedef map<string, RRTypeParamPtr, CIStringLess> StrRRTypeMap;
  70. typedef map<uint16_t, RRTypeParamPtr> CodeRRTypeMap;
  71. inline const string&
  72. RRTypeParam::UNKNOWN_PREFIX() {
  73. static const string p("TYPE");
  74. return (p);
  75. }
  76. inline size_t
  77. RRTypeParam::UNKNOWN_PREFIXLEN() {
  78. static size_t plen = UNKNOWN_PREFIX().size();
  79. return (plen);
  80. }
  81. inline const string&
  82. RRTypeParam::UNKNOWN_MAX() {
  83. static const string p("TYPE65535");
  84. return (p);
  85. }
  86. inline size_t
  87. RRTypeParam::UNKNOWN_MAXLEN() {
  88. static size_t plen = UNKNOWN_MAX().size();
  89. return (plen);
  90. }
  91. struct RRClassParam {
  92. RRClassParam(const string& code_string, uint16_t code) :
  93. code_string_(code_string), code_(code) {}
  94. string code_string_;
  95. uint16_t code_;
  96. /// magic constants
  97. static const unsigned int MAX_CODE = 0xffff;
  98. static const string& UNKNOWN_PREFIX();
  99. static size_t UNKNOWN_PREFIXLEN();
  100. static const string& UNKNOWN_MAX();
  101. static size_t UNKNOWN_MAXLEN();
  102. };
  103. typedef boost::shared_ptr<RRClassParam> RRClassParamPtr;
  104. typedef map<string, RRClassParamPtr, CIStringLess> StrRRClassMap;
  105. typedef map<uint16_t, RRClassParamPtr> CodeRRClassMap;
  106. inline const string&
  107. RRClassParam::UNKNOWN_PREFIX() {
  108. static const string p("CLASS");
  109. return (p);
  110. }
  111. inline size_t
  112. RRClassParam::UNKNOWN_PREFIXLEN() {
  113. static size_t plen = UNKNOWN_PREFIX().size();
  114. return (plen);
  115. }
  116. inline const string&
  117. RRClassParam::UNKNOWN_MAX() {
  118. static const string p("CLASS65535");
  119. return (p);
  120. }
  121. inline size_t
  122. RRClassParam::UNKNOWN_MAXLEN() {
  123. static size_t plen = UNKNOWN_MAX().size();
  124. return (plen);
  125. }
  126. } // end of anonymous namespace
  127. /// Note: the element ordering in the type/class pair is intentional.
  128. /// The standard library will perform inequality comparison (i.e, '<')
  129. /// in the way that the second elements (RRClass) are compared only when
  130. /// the first elements are equivalent.
  131. /// In practice, when we compare two pairs of RRType and RRClass, RRClass
  132. /// would be the same (and, in particular, be class IN) in the majority of
  133. /// cases. So this comparison ordering should be more efficient in common
  134. /// cases.
  135. typedef pair<RRType, RRClass> RRTypeClass;
  136. typedef map<RRTypeClass, RdataFactoryPtr> RdataFactoryMap;
  137. typedef map<RRType, RdataFactoryPtr> GenericRdataFactoryMap;
  138. template <typename T>
  139. class RdataFactory : public AbstractRdataFactory {
  140. public:
  141. virtual RdataPtr create(const string& rdata_str) const
  142. {
  143. return (RdataPtr(new T(rdata_str)));
  144. }
  145. virtual RdataPtr create(InputBuffer& buffer, size_t rdata_len) const
  146. {
  147. return (RdataPtr(new T(buffer, rdata_len)));
  148. }
  149. virtual RdataPtr create(const Rdata& source) const
  150. {
  151. return (RdataPtr(new T(dynamic_cast<const T&>(source))));
  152. }
  153. };
  154. ///
  155. /// \brief The \c RRParamRegistryImpl class is the actual implementation of
  156. /// \c RRParamRegistry.
  157. ///
  158. /// The implementation is hidden from applications. We can refer to specific
  159. /// members of this class only within the implementation source file.
  160. ///
  161. struct RRParamRegistryImpl {
  162. /// Mappings from RR type codes to textual representations.
  163. StrRRTypeMap str2typemap;
  164. /// Mappings from textual representations of RR types to integer codes.
  165. CodeRRTypeMap code2typemap;
  166. /// Mappings from RR class codes to textual representations.
  167. StrRRClassMap str2classmap;
  168. /// Mappings from textual representations of RR classes to integer codes.
  169. CodeRRClassMap code2classmap;
  170. RdataFactoryMap rdata_factories;
  171. GenericRdataFactoryMap genericrdata_factories;
  172. };
  173. RRParamRegistry::RRParamRegistry() {
  174. impl_ = new RRParamRegistryImpl;
  175. // set up parameters for well-known RRs
  176. try {
  177. // BEGIN_WELL_KNOWN_PARAMS
  178. // END_WELL_KNOWN_PARAMS
  179. } catch (...) {
  180. delete impl_;
  181. throw;
  182. }
  183. }
  184. RRParamRegistry::~RRParamRegistry() {
  185. delete impl_;
  186. }
  187. RRParamRegistry&
  188. RRParamRegistry::getRegistry() {
  189. static RRParamRegistry registry;
  190. return (registry);
  191. }
  192. void
  193. RRParamRegistry::add(const std::string& typecode_string, uint16_t typecode,
  194. RdataFactoryPtr rdata_factory)
  195. {
  196. bool type_added = false;
  197. try {
  198. type_added = addType(typecode_string, typecode);
  199. impl_->genericrdata_factories.insert(pair<RRType, RdataFactoryPtr>(
  200. RRType(typecode),
  201. rdata_factory));
  202. } catch (...) {
  203. if (type_added) {
  204. removeType(typecode);
  205. }
  206. throw;
  207. }
  208. }
  209. void
  210. RRParamRegistry::add(const std::string& typecode_string, uint16_t typecode,
  211. const std::string& classcode_string, uint16_t classcode,
  212. RdataFactoryPtr rdata_factory)
  213. {
  214. // Rollback logic on failure is complicated. If adding the new type or
  215. // class fails, we should revert to the original state, cleaning up
  216. // intermediate state. But we need to make sure that we don't remove
  217. // existing data. addType()/addClass() will simply ignore an attempt to
  218. // add the same data, so the cleanup should be performed only when we add
  219. // something new but we fail in other part of the process.
  220. bool type_added = false;
  221. bool class_added = false;
  222. try {
  223. type_added = addType(typecode_string, typecode);
  224. class_added = addClass(classcode_string, classcode);
  225. impl_->rdata_factories.insert(pair<RRTypeClass, RdataFactoryPtr>(
  226. RRTypeClass(RRType(typecode),
  227. RRClass(classcode)),
  228. rdata_factory));
  229. } catch (...) {
  230. if (type_added) {
  231. removeType(typecode);
  232. }
  233. if (class_added) {
  234. removeClass(classcode);
  235. }
  236. throw;
  237. }
  238. }
  239. bool
  240. RRParamRegistry::removeRdataFactory(const RRType& rrtype,
  241. const RRClass& rrclass)
  242. {
  243. RdataFactoryMap::iterator found =
  244. impl_->rdata_factories.find(RRTypeClass(rrtype, rrclass));
  245. if (found != impl_->rdata_factories.end()) {
  246. impl_->rdata_factories.erase(found);
  247. return (true);
  248. }
  249. return (false);
  250. }
  251. bool
  252. RRParamRegistry::removeRdataFactory(const RRType& rrtype) {
  253. GenericRdataFactoryMap::iterator found =
  254. impl_->genericrdata_factories.find(rrtype);
  255. if (found != impl_->genericrdata_factories.end()) {
  256. impl_->genericrdata_factories.erase(found);
  257. return (true);
  258. }
  259. return (false);
  260. }
  261. namespace {
  262. ///
  263. /// These are helper functions to implement case-insensitive string comparison.
  264. /// This could be simplified using strncasecmp(), but unfortunately it's not
  265. /// included in <cstring>. To be as much as portable within the C++ standard
  266. /// we take the "in house" approach here.
  267. ///
  268. bool CICharEqual(char c1, char c2) {
  269. return (tolower(static_cast<unsigned char>(c1)) ==
  270. tolower(static_cast<unsigned char>(c2)));
  271. }
  272. bool
  273. caseStringEqual(const string& s1, const string& s2, size_t n) {
  274. assert(s1.size() >= n && s2.size() >= n);
  275. return (mismatch(s1.begin(), s1.begin() + n, s2.begin(), CICharEqual).first
  276. == s1.begin() + n);
  277. }
  278. /// Code logic for RRTypes and RRClasses is mostly common except (C++) type and
  279. /// member names. So we define type-independent templates to describe the
  280. /// common logic and let concrete classes use it to avoid code duplicates.
  281. /// The following summarize template parameters used in the set of template
  282. /// functions:
  283. /// PT: parameter type, either RRTypeParam or RRClassParam
  284. /// MC: type of mapping class from code: either CodeRRTypeMap or CodeRRClassMap
  285. /// MS: type of mapping class from string: either StrRRTypeMap or StrRRClassMap
  286. /// ET: exception type for error handling: either InvalidRRType or
  287. /// InvalidRRClass
  288. template <typename PT, typename MC, typename MS, typename ET>
  289. inline bool
  290. addParam(const string& code_string, uint16_t code, MC& codemap, MS& stringmap)
  291. {
  292. // Duplicate type check
  293. typename MC::const_iterator found = codemap.find(code);
  294. if (found != codemap.end()) {
  295. if (found->second->code_string_ != code_string) {
  296. isc_throw(ET, "Duplicate RR parameter registration");
  297. }
  298. return (false);
  299. }
  300. typedef boost::shared_ptr<PT> ParamPtr;
  301. typedef pair<string, ParamPtr> StrParamPair;
  302. typedef pair<uint16_t, ParamPtr> CodeParamPair;
  303. ParamPtr param = ParamPtr(new PT(code_string, code));
  304. try {
  305. stringmap.insert(StrParamPair(code_string, param));
  306. codemap.insert(CodeParamPair(code, param));
  307. } catch (...) {
  308. // Rollback to the previous state: not all of the erase operations will
  309. // find the entry, but we don't care.
  310. stringmap.erase(code_string);
  311. codemap.erase(code);
  312. throw;
  313. }
  314. return (true);
  315. }
  316. template <typename MC, typename MS>
  317. inline bool
  318. removeParam(uint16_t code, MC& codemap, MS& stringmap) {
  319. typename MC::iterator found = codemap.find(code);
  320. if (found != codemap.end()) {
  321. size_t erased = stringmap.erase(found->second->code_string_);
  322. // We must have a corresponding entry of the str2 map exists
  323. assert(erased == 1);
  324. codemap.erase(found);
  325. return (true);
  326. }
  327. return (false);
  328. }
  329. template <typename PT, typename MS, typename ET>
  330. inline uint16_t
  331. textToCode(const string& code_str, MS& stringmap) {
  332. typename MS::const_iterator found;
  333. found = stringmap.find(code_str);
  334. if (found != stringmap.end()) {
  335. return (found->second->code_);
  336. }
  337. size_t l = code_str.size();
  338. if (l > PT::UNKNOWN_PREFIXLEN() &&
  339. l <= PT::UNKNOWN_MAXLEN() &&
  340. caseStringEqual(code_str, PT::UNKNOWN_PREFIX(),
  341. PT::UNKNOWN_PREFIXLEN())) {
  342. unsigned int code;
  343. istringstream iss(code_str.substr(PT::UNKNOWN_PREFIXLEN(),
  344. l - PT::UNKNOWN_PREFIXLEN()));
  345. iss >> dec >> code;
  346. if (iss.rdstate() == ios::eofbit && code <= PT::MAX_CODE) {
  347. return (code);
  348. }
  349. }
  350. isc_throw(ET, "Unrecognized RR parameter string: " + code_str);
  351. }
  352. template <typename PT, typename MC>
  353. inline string
  354. codeToText(uint16_t code, MC& codemap) {
  355. typename MC::const_iterator found;
  356. found = codemap.find(code);
  357. if (found != codemap.end()) {
  358. return (found->second->code_string_);
  359. }
  360. ostringstream ss;
  361. ss << code;
  362. return (PT::UNKNOWN_PREFIX() + ss.str());
  363. }
  364. }
  365. bool
  366. RRParamRegistry::addType(const string& type_string, uint16_t code) {
  367. return (addParam<RRTypeParam, CodeRRTypeMap, StrRRTypeMap, RRTypeExists>
  368. (type_string, code, impl_->code2typemap, impl_->str2typemap));
  369. }
  370. bool
  371. RRParamRegistry::removeType(uint16_t code) {
  372. return (removeParam<CodeRRTypeMap, StrRRTypeMap>(code, impl_->code2typemap,
  373. impl_->str2typemap));
  374. }
  375. uint16_t
  376. RRParamRegistry::textToTypeCode(const string& type_string) const {
  377. return (textToCode<RRTypeParam, StrRRTypeMap,
  378. InvalidRRType>(type_string, impl_->str2typemap));
  379. }
  380. string
  381. RRParamRegistry::codeToTypeText(uint16_t code) const {
  382. return (codeToText<RRTypeParam, CodeRRTypeMap>(code, impl_->code2typemap));
  383. }
  384. bool
  385. RRParamRegistry::addClass(const string& class_string, uint16_t code) {
  386. return (addParam<RRClassParam, CodeRRClassMap, StrRRClassMap, RRClassExists>
  387. (class_string, code, impl_->code2classmap, impl_->str2classmap));
  388. }
  389. bool
  390. RRParamRegistry::removeClass(uint16_t code) {
  391. return (removeParam<CodeRRClassMap, StrRRClassMap>(code,
  392. impl_->code2classmap,
  393. impl_->str2classmap));
  394. }
  395. uint16_t
  396. RRParamRegistry::textToClassCode(const string& class_string) const {
  397. return (textToCode<RRClassParam, StrRRClassMap,
  398. InvalidRRClass>(class_string, impl_->str2classmap));
  399. }
  400. string
  401. RRParamRegistry::codeToClassText(uint16_t code) const {
  402. return (codeToText<RRClassParam, CodeRRClassMap>(code,
  403. impl_->code2classmap));
  404. }
  405. RdataPtr
  406. RRParamRegistry::createRdata(const RRType& rrtype, const RRClass& rrclass,
  407. const std::string& rdata_string)
  408. {
  409. // If the text indicates that it's rdata of an "unknown" type (beginning
  410. // with '\# n'), parse it that way. (TBD)
  411. RdataFactoryMap::const_iterator found;
  412. found = impl_->rdata_factories.find(RRTypeClass(rrtype, rrclass));
  413. if (found != impl_->rdata_factories.end()) {
  414. return (found->second->create(rdata_string));
  415. }
  416. GenericRdataFactoryMap::const_iterator genfound =
  417. impl_->genericrdata_factories.find(rrtype);
  418. if (genfound != impl_->genericrdata_factories.end()) {
  419. return (genfound->second->create(rdata_string));
  420. }
  421. return (RdataPtr(new generic::Generic(rdata_string)));
  422. }
  423. RdataPtr
  424. RRParamRegistry::createRdata(const RRType& rrtype, const RRClass& rrclass,
  425. InputBuffer& buffer, size_t rdata_len)
  426. {
  427. RdataFactoryMap::const_iterator found =
  428. impl_->rdata_factories.find(RRTypeClass(rrtype, rrclass));
  429. if (found != impl_->rdata_factories.end()) {
  430. return (found->second->create(buffer, rdata_len));
  431. }
  432. GenericRdataFactoryMap::const_iterator genfound =
  433. impl_->genericrdata_factories.find(rrtype);
  434. if (genfound != impl_->genericrdata_factories.end()) {
  435. return (genfound->second->create(buffer, rdata_len));
  436. }
  437. return (RdataPtr(new generic::Generic(buffer, rdata_len)));
  438. }
  439. RdataPtr
  440. RRParamRegistry::createRdata(const RRType& rrtype, const RRClass& rrclass,
  441. const Rdata& source)
  442. {
  443. RdataFactoryMap::const_iterator found =
  444. impl_->rdata_factories.find(RRTypeClass(rrtype, rrclass));
  445. if (found != impl_->rdata_factories.end()) {
  446. return (found->second->create(source));
  447. }
  448. GenericRdataFactoryMap::const_iterator genfound =
  449. impl_->genericrdata_factories.find(rrtype);
  450. if (genfound != impl_->genericrdata_factories.end()) {
  451. return (genfound->second->create(source));
  452. }
  453. return (RdataPtr(new rdata::generic::Generic(
  454. dynamic_cast<const generic::Generic&>(source))));
  455. }
  456. }
  457. }