message.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. // Copyright (C) 2009 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. // $Id$
  15. #ifndef __MESSAGE_H
  16. #define __MESSAGE_H 1
  17. #include <iterator>
  18. #include <string>
  19. #include <exceptions/exceptions.h>
  20. #include "question.h"
  21. #include "rrset.h"
  22. namespace isc {
  23. namespace dns {
  24. ///
  25. /// \brief A standard DNS module exception ...[TBD]
  26. ///
  27. class Rcode; // forward declaration
  28. class DNSProtocolError : public Exception {
  29. public:
  30. DNSProtocolError(const char* file, size_t line, const char* what) :
  31. isc::Exception(file, line, what) {}
  32. virtual const Rcode& getRcode() const = 0;
  33. };
  34. class DNSMessageFORMERR : public DNSProtocolError {
  35. public:
  36. DNSMessageFORMERR(const char* file, size_t line, const char* what) :
  37. DNSProtocolError(file, line, what) {}
  38. virtual const Rcode& getRcode() const;
  39. };
  40. class DNSMessageBADVERS : public DNSProtocolError {
  41. public:
  42. DNSMessageBADVERS(const char* file, size_t line, const char* what) :
  43. DNSProtocolError(file, line, what) {}
  44. virtual const Rcode& getRcode() const;
  45. };
  46. ///
  47. /// \brief A standard DNS module exception ...[TBD]
  48. ///
  49. class MessageTooShort : public Exception {
  50. public:
  51. MessageTooShort(const char* file, size_t line, const char* what) :
  52. isc::Exception(file, line, what) {}
  53. };
  54. class InvalidMessageSection : public Exception {
  55. public:
  56. InvalidMessageSection(const char* file, size_t line, const char* what) :
  57. isc::Exception(file, line, what) {}
  58. };
  59. class InvalidMessageOperation : public Exception {
  60. public:
  61. InvalidMessageOperation(const char* file, size_t line, const char* what) :
  62. isc::Exception(file, line, what) {}
  63. };
  64. class InvalidMessageUDPSize : public Exception {
  65. public:
  66. InvalidMessageUDPSize(const char* file, size_t line, const char* what) :
  67. isc::Exception(file, line, what) {}
  68. };
  69. typedef uint8_t rcode_t; // we actually only need 4 bits of it
  70. typedef uint8_t opcode_t; // we actually only need 4 bits of it
  71. typedef uint16_t qid_t;
  72. class InputBuffer;
  73. class MessageRenderer;
  74. class Message;
  75. struct MessageImpl;
  76. template <typename T>
  77. struct SectionIteratorImpl;
  78. class MessageFlag {
  79. public:
  80. uint16_t getBit() const { return (flagbit_); }
  81. static const MessageFlag& QR();
  82. static const MessageFlag& AA();
  83. static const MessageFlag& TC();
  84. static const MessageFlag& RD();
  85. static const MessageFlag& RA();
  86. static const MessageFlag& AD();
  87. static const MessageFlag& CD();
  88. private:
  89. MessageFlag(uint16_t flagbit) : flagbit_(flagbit) {}
  90. uint16_t flagbit_;
  91. };
  92. inline const MessageFlag&
  93. MessageFlag::QR()
  94. {
  95. static MessageFlag f(0x8000);
  96. return (f);
  97. }
  98. inline const MessageFlag&
  99. MessageFlag::AA()
  100. {
  101. static MessageFlag f(0x0400);
  102. return (f);
  103. }
  104. inline const MessageFlag&
  105. MessageFlag::TC()
  106. {
  107. static MessageFlag f(0x0200);
  108. return (f);
  109. }
  110. inline const MessageFlag&
  111. MessageFlag::RD()
  112. {
  113. static MessageFlag f(0x0100);
  114. return (f);
  115. }
  116. inline const MessageFlag&
  117. MessageFlag::RA()
  118. {
  119. static MessageFlag f(0x0080);
  120. return (f);
  121. }
  122. inline const MessageFlag&
  123. MessageFlag::AD()
  124. {
  125. static MessageFlag f(0x0020);
  126. return (f);
  127. }
  128. inline const MessageFlag&
  129. MessageFlag::CD()
  130. {
  131. static MessageFlag f(0x0010);
  132. return (f);
  133. }
  134. class Opcode {
  135. public:
  136. uint16_t getCode() const { return (code_); }
  137. bool operator==(const Opcode& other) const
  138. { return (code_ == other.code_); }
  139. bool operator!=(const Opcode& other) const
  140. { return (code_ != other.code_); }
  141. std::string toText() const;
  142. static const Opcode& QUERY();
  143. static const Opcode& IQUERY();
  144. static const Opcode& STATUS();
  145. static const Opcode& RESERVED3();
  146. static const Opcode& NOTIFY();
  147. static const Opcode& UPDATE();
  148. static const Opcode& RESERVED6();
  149. static const Opcode& RESERVED7();
  150. static const Opcode& RESERVED8();
  151. static const Opcode& RESERVED9();
  152. static const Opcode& RESERVED10();
  153. static const Opcode& RESERVED11();
  154. static const Opcode& RESERVED12();
  155. static const Opcode& RESERVED13();
  156. static const Opcode& RESERVED14();
  157. static const Opcode& RESERVED15();
  158. private:
  159. Opcode(uint16_t code) : code_(code) {}
  160. uint16_t code_;
  161. };
  162. inline const Opcode&
  163. Opcode::QUERY()
  164. {
  165. static Opcode c(0);
  166. return (c);
  167. }
  168. inline const Opcode&
  169. Opcode::IQUERY()
  170. {
  171. static Opcode c(1);
  172. return (c);
  173. }
  174. inline const Opcode&
  175. Opcode::STATUS()
  176. {
  177. static Opcode c(2);
  178. return (c);
  179. }
  180. inline const Opcode&
  181. Opcode::RESERVED3()
  182. {
  183. static Opcode c(3);
  184. return (c);
  185. }
  186. inline const Opcode&
  187. Opcode::NOTIFY()
  188. {
  189. static Opcode c(4);
  190. return (c);
  191. }
  192. inline const Opcode&
  193. Opcode::UPDATE()
  194. {
  195. static Opcode c(5);
  196. return (c);
  197. }
  198. inline const Opcode&
  199. Opcode::RESERVED6()
  200. {
  201. static Opcode c(6);
  202. return (c);
  203. }
  204. inline const Opcode&
  205. Opcode::RESERVED7()
  206. {
  207. static Opcode c(7);
  208. return (c);
  209. }
  210. inline const Opcode&
  211. Opcode::RESERVED8()
  212. {
  213. static Opcode c(8);
  214. return (c);
  215. }
  216. inline const Opcode&
  217. Opcode::RESERVED9()
  218. {
  219. static Opcode c(9);
  220. return (c);
  221. }
  222. inline const Opcode&
  223. Opcode::RESERVED10()
  224. {
  225. static Opcode c(10);
  226. return (c);
  227. }
  228. inline const Opcode&
  229. Opcode::RESERVED11()
  230. {
  231. static Opcode c(11);
  232. return (c);
  233. }
  234. inline const Opcode&
  235. Opcode::RESERVED12()
  236. {
  237. static Opcode c(12);
  238. return (c);
  239. }
  240. inline const Opcode&
  241. Opcode::RESERVED13()
  242. {
  243. static Opcode c(13);
  244. return (c);
  245. }
  246. inline const Opcode&
  247. Opcode::RESERVED14()
  248. {
  249. static Opcode c(14);
  250. return (c);
  251. }
  252. inline const Opcode&
  253. Opcode::RESERVED15()
  254. {
  255. static Opcode c(15);
  256. return (c);
  257. }
  258. class Rcode {
  259. public:
  260. Rcode(uint16_t code);
  261. uint16_t getCode() const { return (code_); }
  262. bool operator==(const Rcode& other) const { return (code_ == other.code_); }
  263. std::string toText() const;
  264. static const Rcode& NOERROR();
  265. static const Rcode& FORMERR();
  266. static const Rcode& SERVFAIL();
  267. static const Rcode& NXDOMAIN();
  268. static const Rcode& NOTIMP();
  269. static const Rcode& REFUSED();
  270. static const Rcode& YXDOMAIN();
  271. static const Rcode& YXRRSET();
  272. static const Rcode& NXRRSET();
  273. static const Rcode& NOTAUTH();
  274. static const Rcode& NOTZONE();
  275. static const Rcode& RESERVED11();
  276. static const Rcode& RESERVED12();
  277. static const Rcode& RESERVED13();
  278. static const Rcode& RESERVED14();
  279. static const Rcode& RESERVED15();
  280. // Extended Rcodes follow (EDNS required)
  281. static const Rcode& BADVERS();
  282. private:
  283. uint16_t code_;
  284. // EDNS-extended RCODEs are 12-bit unsigned integers.
  285. static const uint16_t MAX_RCODE = 0xfff;
  286. };
  287. inline const Rcode&
  288. Rcode::NOERROR()
  289. {
  290. static Rcode c(0);
  291. return (c);
  292. }
  293. inline const Rcode&
  294. Rcode::FORMERR()
  295. {
  296. static Rcode c(1);
  297. return (c);
  298. }
  299. inline const Rcode&
  300. Rcode::SERVFAIL()
  301. {
  302. static Rcode c(2);
  303. return (c);
  304. }
  305. inline const Rcode&
  306. Rcode::NXDOMAIN()
  307. {
  308. static Rcode c(3);
  309. return (c);
  310. }
  311. inline const Rcode&
  312. Rcode::NOTIMP()
  313. {
  314. static Rcode c(4);
  315. return (c);
  316. }
  317. inline const Rcode&
  318. Rcode::REFUSED()
  319. {
  320. static Rcode c(5);
  321. return (c);
  322. }
  323. inline const Rcode&
  324. Rcode::YXDOMAIN()
  325. {
  326. static Rcode c(6);
  327. return (c);
  328. }
  329. inline const Rcode&
  330. Rcode::YXRRSET()
  331. {
  332. static Rcode c(7);
  333. return (c);
  334. }
  335. inline const Rcode&
  336. Rcode::NXRRSET()
  337. {
  338. static Rcode c(8);
  339. return (c);
  340. }
  341. inline const Rcode&
  342. Rcode::NOTAUTH()
  343. {
  344. static Rcode c(9);
  345. return (c);
  346. }
  347. inline const Rcode&
  348. Rcode::NOTZONE()
  349. {
  350. static Rcode c(10);
  351. return (c);
  352. }
  353. inline const Rcode&
  354. Rcode::RESERVED11()
  355. {
  356. static Rcode c(11);
  357. return (c);
  358. }
  359. inline const Rcode&
  360. Rcode::RESERVED12()
  361. {
  362. static Rcode c(12);
  363. return (c);
  364. }
  365. inline const Rcode&
  366. Rcode::RESERVED13()
  367. {
  368. static Rcode c(13);
  369. return (c);
  370. }
  371. inline const Rcode&
  372. Rcode::RESERVED14()
  373. {
  374. static Rcode c(14);
  375. return (c);
  376. }
  377. inline const Rcode&
  378. Rcode::RESERVED15()
  379. {
  380. static Rcode c(15);
  381. return (c);
  382. }
  383. inline const Rcode&
  384. Rcode::BADVERS()
  385. {
  386. static Rcode c(16);
  387. return (c);
  388. }
  389. class Section {
  390. public:
  391. /// \brief Returns the relative position of the \c Section in DNS messages.
  392. unsigned int getCode() const { return (code_); }
  393. bool operator==(const Section& other) const
  394. { return (code_ == other.code_); }
  395. bool operator!=(const Section& other) const
  396. { return (code_ != other.code_); }
  397. static const Section& QUESTION();
  398. static const Section& ANSWER();
  399. static const Section& AUTHORITY();
  400. static const Section& ADDITIONAL();
  401. private:
  402. enum {
  403. SECTION_QUESTION = 0,
  404. SECTION_ANSWER = 1,
  405. SECTION_AUTHORITY = 2,
  406. SECTION_ADDITIONAL = 3
  407. };
  408. Section(int code) : code_(code) {}
  409. unsigned int code_;
  410. };
  411. inline const Section&
  412. Section::QUESTION()
  413. {
  414. static Section s(SECTION_QUESTION);
  415. return (s);
  416. }
  417. inline const Section&
  418. Section::ANSWER()
  419. {
  420. static Section s(SECTION_ANSWER);
  421. return (s);
  422. }
  423. inline const Section&
  424. Section::AUTHORITY()
  425. {
  426. static Section s(SECTION_AUTHORITY);
  427. return (s);
  428. }
  429. inline const Section&
  430. Section::ADDITIONAL()
  431. {
  432. static Section s(SECTION_ADDITIONAL);
  433. return (s);
  434. }
  435. template <typename T>
  436. class SectionIterator : public std::iterator<std::input_iterator_tag, T> {
  437. public:
  438. SectionIterator<T>() : impl_(NULL) {}
  439. SectionIterator<T>(const SectionIteratorImpl<T>& impl);
  440. ~SectionIterator<T>();
  441. SectionIterator<T>(const SectionIterator<T>& source);
  442. void operator=(const SectionIterator<T>& source);
  443. SectionIterator<T>& operator++();
  444. SectionIterator<T> operator++(int);
  445. const T& operator*() const;
  446. const T* operator->() const;
  447. bool operator!=(const SectionIterator<T>& other) const;
  448. private:
  449. SectionIteratorImpl<T>* impl_;
  450. };
  451. typedef SectionIterator<QuestionPtr> QuestionIterator;
  452. typedef SectionIterator<RRsetPtr> RRsetIterator;
  453. class Message {
  454. public:
  455. enum Mode {
  456. PARSE = 0,
  457. RENDER = 1
  458. };
  459. public:
  460. Message(Mode mode);
  461. ~Message();
  462. private:
  463. Message(const Message& source);
  464. Message& operator=(const Message& source);
  465. public:
  466. bool getHeaderFlag(const MessageFlag& flag) const;
  467. void setHeaderFlag(const MessageFlag& flag);
  468. void clearHeaderFlag(const MessageFlag& flag);
  469. bool isDNSSECSupported() const;
  470. void setDNSSECSupported(bool on);
  471. uint16_t getUDPSize() const;
  472. void setUDPSize(uint16_t size);
  473. qid_t getQid() const;
  474. void setQid(qid_t qid);
  475. const Rcode& getRcode() const;
  476. void setRcode(const Rcode& rcode);
  477. const Opcode& getOpcode() const;
  478. void setOpcode(const Opcode& opcode);
  479. std::string toText() const;
  480. /// \brief Returns the number of RRs contained in the given section.
  481. unsigned int getRRCount(const Section& section) const;
  482. // we don't provide accessors to QD/AN/NS/AR counters as this information
  483. // is included in the corresponding RRsets.
  484. // Open issues:
  485. // - may want to provide an "iterator" for all RRsets/RRs
  486. // - may want to provide a "find" method for a specified type
  487. // of RR in the message
  488. const QuestionIterator beginQuestion() const;
  489. const QuestionIterator endQuestion() const;
  490. const RRsetIterator beginSection(const Section& section) const;
  491. const RRsetIterator endSection(const Section& section) const;
  492. void addQuestion(QuestionPtr question);
  493. void addQuestion(const Question& question);
  494. void removeQuestion(QuestionPtr question);
  495. void addRRset(const Section& section, RRsetPtr rrset, bool sign = false);
  496. void removeRRset(const Section& section, RRsetPtr rrset);
  497. // notyet:
  498. //void addRR(const Section& section, const RR& rr);
  499. //void removeRR(const Section& section, const RR& rr);
  500. void clear(Mode mode);
  501. // prepare for making a response from a request. This will clear the
  502. // DNS header except those fields that should be kept for the response,
  503. // and clear answer and the following sections.
  504. // see also dns_message_reply() of BIND9.
  505. void makeResponse();
  506. /// \brief Render message.
  507. void toWire(MessageRenderer& renderer);
  508. /// \brief Parse a DNS message.
  509. void fromWire(InputBuffer& buffer);
  510. ///
  511. /// \name Protocol constants
  512. ///
  513. //@{
  514. /// \brief The default maximum size of UDP DNS messages that don't cause
  515. /// truncation.
  516. ///
  517. /// With EDNS the maximum size can be increases per message.
  518. static const uint16_t DEFAULT_MAX_UDPSIZE = 512;
  519. /// \brief The highest EDNS version this implementation supports.
  520. static const uint8_t EDNS_SUPPORTED_VERSION = 0;
  521. //@}
  522. private:
  523. MessageImpl* impl_;
  524. };
  525. std::ostream& operator<<(std::ostream& os, const Opcode& opcode);
  526. std::ostream& operator<<(std::ostream& os, const Rcode& rcode);
  527. std::ostream& operator<<(std::ostream& os, const Message& message);
  528. }
  529. }
  530. #endif // __MESSAGE_H
  531. // Local Variables:
  532. // mode: c++
  533. // End: