message.h 11 KB

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