rrparamregistry-placeholder.cc 18 KB

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