message.h 13 KB

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