edns.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. // $Id$
  15. #ifndef __EDNS_H
  16. #define __EDNS_H 1
  17. #include <stdint.h>
  18. #include <boost/shared_ptr.hpp>
  19. #include <ostream>
  20. #include <dns/rdata.h>
  21. namespace isc {
  22. namespace dns {
  23. class EDNS;
  24. class Name;
  25. class MessageRenderer;
  26. class RRClass;
  27. class RRTTL;
  28. class RRType;
  29. class Rcode;
  30. /// \brief A pointer-like type pointing to an \c EDNS object.
  31. typedef boost::shared_ptr<EDNS> EDNSPtr;
  32. /// \brief A pointer-like type pointing to an immutable \c EDNS object.
  33. typedef boost::shared_ptr<const EDNS> ConstEDNSPtr;
  34. /// The \c EDNS class represents the %EDNS OPT RR defined in RFC2671.
  35. ///
  36. /// This class encapsulates various optional features of %EDNS such as
  37. /// the UDP payload size or the DNSSEC DO bit, and provides interfaces
  38. /// to manage these features. It is also responsible for conversion
  39. /// to and from wire-format OPT RR.
  40. /// One important exception is about the extended RCODE:
  41. /// The \c EDNS class is only responsible for extracting the 8-bit part
  42. /// of the 12-bit extended RCODE from the OPT RR's TTL field of an
  43. /// incoming message, and for setting the 8-bit part into the OPT RR TTL
  44. /// of an outgoing message. It's not supposed to know how to construct the
  45. /// complete RCODE, much less maintain the RCODE in it.
  46. /// It is the caller's responsibility (typically the \c Message class).
  47. ///
  48. /// When converting wire-format OPT RR into an \c EDNS object, it normalizes
  49. /// the information, i.e., unknown flags will be ignored on construction.
  50. ///
  51. /// This class is also supposed to support %EDNS options such as NSID,
  52. /// but the initial implementation does not include it. This is a near term
  53. /// TODO item.
  54. ///
  55. /// <b>Notes to developers</b>
  56. ///
  57. /// The rest of the description is for developers who need to or want to
  58. /// understand the design of this API.
  59. ///
  60. /// Representing %EDNS is tricky. An OPT RR is no different from other RRs
  61. /// in terms of the wire format syntax, and in that sense we could use the
  62. /// generic \c RRset class to represent an OPT RR (BIND 9 adopts this
  63. /// approach). But the resulting interface would be inconvenient for
  64. /// developers. For example, the developer would need to know that the
  65. /// UDP size is encoded in the RR Class field. It's better to provide
  66. /// a more abstract interface along with the special semantics of OPT RR.
  67. ///
  68. /// Another approach would be to realize each optional feature of EDNS
  69. /// as an attribute of the DNS message.
  70. /// NLnet Labs' ldns takes this approach.
  71. /// This way an operation for specifying the UDP size would be written
  72. /// like this:
  73. /// \code message->setUDPSize(4096); \endcode
  74. /// which should be more intuitive.
  75. /// A drawback of this approach is that OPT RR is itself optional and the
  76. /// separate parameters may not necessarily indicate whether to include an
  77. /// OPT RR per se.
  78. /// For example, consider what should be done with this code:
  79. /// \code message->setUDPSize(512); \endcode
  80. /// Since the payload size of 512 is the default, it may mean the OPT RR
  81. /// should be skipped. But it might also mean the caller intentionally
  82. /// (for some reason) wants to insert an OPT RR specifying the default UDP
  83. /// size explicitly.
  84. ///
  85. /// So, we use a separate class that encapsulates the EDNS semantics and
  86. /// knows the mapping between the semantics and the wire format representation.
  87. /// This way the interface can be semantics-based and is intuitive:
  88. /// \code edns->setUDPSize(4096); \endcode
  89. /// while we can explicitly specify whether to include an OPT RR by setting
  90. /// (or not setting) an \c EDNS object in a message:
  91. /// \code message->setEDNS(edns); // unless we do this OPT RR is skipped
  92. /// \endcode
  93. ///
  94. /// There is still a non trivial point: How to manage extended RCODEs.
  95. /// An OPT RR encodes the upper 8 bits of extended 12-bit RCODE.
  96. /// In general, it would be better to provide a unified interface to get
  97. /// access to RCODEs whether or not they are traditional 4 bit codes or
  98. /// extended ones that have non 0 upper bits.
  99. /// However, since an OPT RR may not appear in a message the RCODE cannot be
  100. /// maintained in the \c EDNS class.
  101. /// But it would not be desirable to maintain the extended RCODEs completely
  102. /// in the \c Message class, either, because we wanted to hide the mapping
  103. /// between %EDNS semantics and its wire format representation within the
  104. /// \c EDNS class; if we moved the responsibility about RCODEs to the
  105. /// \c Message class, it would have to parse and render the upper 8 bits of
  106. /// the RCODEs, dealing with wire representation of OPT RR.
  107. /// This is suboptimal in the sense of encapsulation.
  108. ///
  109. /// As a compromise, our decision is to separate the knowledge about the
  110. /// relationship with RCODE from the knowledge about the wire format as
  111. /// noted in the beginning of this description.
  112. ///
  113. /// This decoupling is based on the observation that the extended RCODE
  114. /// is a very special case where %EDNS only has partial information.
  115. /// If a future version of the %EDNS protocol introduces further relationship
  116. /// between the message and the %EDNS, we might reconsider the interface,
  117. /// probably with higher abstraction.
  118. class EDNS {
  119. public:
  120. ///
  121. /// \name Constructors and Destructor
  122. ///
  123. /// We use the default copy constructor, default copy assignment operator,
  124. /// and default destructors intentionally.
  125. ///
  126. /// Note about copyability: This version of this class is copyable,
  127. /// but we may want to change it once we support EDNS options, when
  128. /// we want to revise this class using the pimpl idiom.
  129. /// But we should be careful about that: the python binding currently
  130. /// assumes this class is copyable.
  131. //@{
  132. /// Constructor with the EDNS version.
  133. /// An application would use this constructor to specify EDNS parameters
  134. /// and/or options for outgoing DNS messages.
  135. ///
  136. /// All other parameters than the version number will be initialized to
  137. /// reasonable defaults.
  138. /// Specifically, the UDP payload size is set to
  139. /// \c Message::DEFAULT_MAX_UDPSIZE, and DNSSEC is assumed to be not
  140. /// supported.
  141. /// These parameters can be altered via setter methods of this class.
  142. /// Note, however, that the version number cannot be changed once
  143. /// constructed.
  144. ///
  145. /// The version number parameter can be omitted, in which case the highest
  146. /// supported version in this implementation will be assumed.
  147. /// When specified, if it is larger than the highest supported version,
  148. /// an exception of class \c isc::InvalidParameter will be thrown.
  149. ///
  150. /// This constructor throws no other exception.
  151. ///
  152. /// \param version The version number of the EDNS to be constructed.
  153. explicit EDNS(const uint8_t version = SUPPORTED_VERSION);
  154. /// \brief Constructor from resource record (RR) parameters.
  155. ///
  156. /// This constructor is intended to be used to construct an EDNS object
  157. /// from an OPT RR contained in an incoming DNS message.
  158. ///
  159. /// Unlike many other constructors for this purpose, this constructor
  160. /// does not take the bare wire-format %data in the form of an
  161. /// \c InputBuffer object. This is because parsing incoming EDNS is
  162. /// highly context dependent and it's not feasible to handle it in a
  163. /// completely polymorphic way. For example, a DNS message parser would
  164. /// have to check an OPT RR appears at most once in the message, and if
  165. /// it appears it should be in the additional section. So, the parser
  166. /// needs to have an explicit check to see if an RR is of type OPT, and
  167. /// then (if other conditions are met) construct a corresponding \c EDNS
  168. /// object. At that point the parser would have already converted the
  169. /// wire %data into corresponding objects of \c Name, \c RRClass,
  170. /// \c RRType, etc, and it makes more sense to pass them directly to the
  171. /// constructor.
  172. ///
  173. /// In practice, top level applications rarely need to use this
  174. /// constructor directly. It should normally suffice to have a higher
  175. /// level class such as \c Message do that job.
  176. ///
  177. /// This constructor checks the passed parameters to see if they are
  178. /// valid in terms of the EDNS protocol specification.
  179. /// \c name must be the root name ("."); otherwise, an exception of
  180. /// class \c DNSMessageFORMERR will be thrown.
  181. /// \c rrtype must specify the OPT RR type; otherwise, an exception of
  182. /// class \c isc::InvalidParameter will be thrown.
  183. /// The ENDS version number is extracted from \c rrttl. If it is larger
  184. /// than the higher supported version, an exception of class
  185. /// \c DNSMessageBADVERS will be thrown. Note that this is different from
  186. /// the case of the same error in the other constructor.
  187. /// This is intentional, so that the application can transparently convert
  188. /// the exception to a response RCODE according to the protocol
  189. /// specification.
  190. ///
  191. /// This initial implementation does not support EDNS options at all,
  192. /// and \c rdata is simply ignored. Future versions will support
  193. /// options, and may throw exceptions while validating the given parameter.
  194. ///
  195. /// \b Note: since no other type than OPT for \c rrtype is allowed, this
  196. /// parameter could actually have been omitted. But it is intentionally
  197. /// included as a parameter so that invalid usage of the construction
  198. /// can be detected. As noted above the caller should normally have
  199. /// the corresponding \c RRType object at the time of call to this
  200. /// constructor, so the overhead of having the additional parameter
  201. /// should be marginal.
  202. ///
  203. /// \param name The owner name of the OPT RR. This must be the root name.
  204. /// \param rrclass The RR class of the OPT RR.
  205. /// \param rrtype This must specify the OPT RR type.
  206. /// \param rrttl The TTL of the OPT RR.
  207. /// \param rdata The RDATA of the OPT RR.
  208. EDNS(const Name& name, const RRClass& rrclass, const RRType& rrtype,
  209. const RRTTL& ttl, const rdata::Rdata& rdata);
  210. //@}
  211. ///
  212. /// \name Getter and Setter Methods
  213. ///
  214. //@{
  215. /// \brief Returns the version of EDNS.
  216. ///
  217. /// This method never throws an exception.
  218. uint8_t getVersion() const { return (version_); }
  219. /// \brief Returns the maximum payload size of UDP messages for the sender
  220. /// of the message containing this \c EDNS.
  221. ///
  222. /// This method never throws an exception.
  223. uint16_t getUDPSize() const { return (udp_size_); }
  224. /// \brief Specify the maximum payload size of UDP messages that use
  225. /// this EDNS.
  226. ///
  227. /// Unless explicitly specified, \c DEFAULT_MAX_UDPSIZE will be assumed
  228. /// for the maximum payload size, regardless of whether EDNS OPT RR is
  229. /// included or not. This means if an application wants to send a message
  230. /// with an EDNS OPT RR for specifying a larger UDP size, it must
  231. /// explicitly specify the value using this method.
  232. ///
  233. /// This method never throws an exception.
  234. ///
  235. /// \param udp_size The maximum payload size of UDP messages for the sender
  236. /// of the message containing this \c EDNS.
  237. void setUDPSize(const uint16_t udp_size) { udp_size_ = udp_size; }
  238. /// \brief Returns whether the message sender is DNSSEC aware.
  239. ///
  240. /// This method never throws an exception.
  241. ///
  242. /// \return true if DNSSEC is supported; otherwise false.
  243. bool getDNSSECAwareness() const { return (dnssec_aware_); }
  244. /// \brief Specifies whether the sender of the message containing this
  245. /// \c EDNS is DNSSEC aware.
  246. ///
  247. /// If the parameter is true, a subsequent call to \c toWire() will
  248. /// set the DNSSEC DO bit on for the corresponding OPT RR.
  249. ///
  250. /// This method never throws an exception.
  251. ///
  252. /// \param is_aware \c true if DNSSEC is supported; \c false otherwise.
  253. void setDNSSECAwareness(const bool is_aware) { dnssec_aware_ = is_aware; }
  254. //@}
  255. ///
  256. /// \name Converter Methods
  257. ///
  258. //@{
  259. /// \brief Render the \c EDNS in the wire format.
  260. ///
  261. /// This method renders the \c EDNS object as a form of DNS OPT RR
  262. /// via \c renderer, which encapsulates output buffer and other rendering
  263. /// contexts.
  264. /// Since the \c EDNS object does not maintain the extended RCODE
  265. /// information, a separate parameter \c extended_rcode must be passed to
  266. /// this method.
  267. ///
  268. /// If by adding the OPT RR the message size would exceed the limit
  269. /// maintained in \c renderer, this method skips rendering the RR
  270. /// and returns 0; otherwise it returns 1, which is the number of RR
  271. /// rendered.
  272. ///
  273. /// In the current implementation the return value is either 0 or 1, but
  274. /// the return type is <code>unsigned int</code> to be consistent with
  275. /// \c RRset::toWire(). In any case the caller shouldn't assume these are
  276. /// only possible return values from this method.
  277. ///
  278. /// This method is mostly exception free, but it requires memory
  279. /// allocation and if it fails a corresponding standard exception will be
  280. /// thrown.
  281. ///
  282. /// In practice, top level applications rarely need to use this
  283. /// method directly. It should normally suffice to have a higher
  284. /// level class such as \c Message do that job.
  285. ///
  286. /// <b>Note to developer:</b> the current implementation constructs an
  287. /// \c RRset object for the OPT RR and calls its \c toWire() method,
  288. /// which is inefficient. In future, we may want to optimize this method
  289. /// by caching the rendered image and having the application reuse the
  290. /// same \c EDNS object when possible.
  291. ///
  292. /// \param renderer DNS message rendering context that encapsulates the
  293. /// output buffer and name compression information.
  294. /// \param extended_rcode Upper 8 bits of extended RCODE to be rendered as
  295. /// part of the EDNS OPT RR.
  296. /// \return 1 if the OPT RR fits in the message size limit; otherwise 0.
  297. unsigned int toWire(MessageRenderer& renderer,
  298. const uint8_t extended_rcode) const;
  299. /// \brief Render the \c EDNS in the wire format.
  300. ///
  301. /// This method is same as \c toWire(MessageRenderer&,uint8_t)const
  302. /// except it renders the OPT RR in an \c OutputBuffer and therefore
  303. /// does not care about message size limit.
  304. /// As a consequence it always returns 1.
  305. unsigned int toWire(OutputBuffer& buffer,
  306. const uint8_t extended_rcode) const;
  307. /// \brief Convert the EDNS to a string.
  308. ///
  309. /// The format of the resulting string is as follows:
  310. /// \code ; EDNS: version: <version>, flags: <edns flags>; udp: <udp size>
  311. /// \endcode
  312. /// where
  313. /// - \em version is the EDNS version number (integer).
  314. /// - <em>edns flags</em> is a sequence of EDNS flag bits. The only
  315. /// possible flag is the "DNSSEC OK", which is represented as "do".
  316. /// - <em>udp size</em> is sender's UDP payload size in bytes.
  317. ///
  318. /// The string will be terminated with a trailing newline character.
  319. ///
  320. /// When EDNS options are supported the output of this method will be
  321. /// extended.
  322. ///
  323. /// This method is mostly exception free, but it may require memory
  324. /// allocation and if it fails a corresponding standard exception will be
  325. /// thrown.
  326. ///
  327. /// \return A string representation of \c EDNS. See above for the format.
  328. std::string toText() const;
  329. //@}
  330. // TBD: This method is currently not implemented. We'll eventually need
  331. // something like this.
  332. //void addOption();
  333. private:
  334. /// Helper method to define unified implementation for the public versions
  335. /// of toWire().
  336. template <typename Output>
  337. int toWire(Output& output, const uint8_t extended_rcode) const;
  338. public:
  339. /// \brief The highest EDNS version this implementation supports.
  340. static const uint8_t SUPPORTED_VERSION = 0;
  341. private:
  342. // We may eventually want to migrate to pimpl, especially when we support
  343. // EDNS options. In this initial implementation, we keep it simple.
  344. const uint8_t version_;
  345. uint16_t udp_size_;
  346. bool dnssec_aware_;
  347. };
  348. /// \brief Create a new \c EDNS object from a set of RR parameters, also
  349. /// providing the extended RCODE value.
  350. ///
  351. /// This function is similar to the EDNS class constructor
  352. /// \c EDNS::EDNS(const Name&, const RRClass&, const RRType&, const RRTTL&, const rdata::Rdata&)
  353. /// but is different in that
  354. /// - It dynamically creates a new object
  355. /// - It returns (via a reference argument) the topmost 8 bits of the extended
  356. /// RCODE encoded in the \c ttl.
  357. ///
  358. /// On success, \c extended_rcode will be updated with the 8-bit part of
  359. /// the extended RCODE encoded in the TTL of the OPT RR.
  360. ///
  361. /// The intended usage of this function is to parse an OPT RR of an incoming
  362. /// DNS message, while updating the RCODE of the message.
  363. /// One common usage patter is as follows:
  364. ///
  365. /// \code Message msg;
  366. /// ...
  367. /// uint8_t extended_rcode;
  368. /// ConstEDNSPtr edns = ConstEDNSPtr(createEDNSFromRR(..., extended_rcode));
  369. /// rcode = Rcode(msg.getRcode().getCode(), extended_rcode);
  370. /// \endcode
  371. /// (although, like the \c EDNS constructor, normal applications wouldn't have
  372. /// to use this function directly).
  373. ///
  374. /// This function provides the strong exception guarantee: Unless an
  375. /// exception is thrown \c extended_code won't be modified.
  376. ///
  377. /// This function validates the given parameters and throws exceptions on
  378. /// failure in the same way as the \c EDNS class constructor.
  379. /// In addition, if memory allocation for the new object fails it throws the
  380. /// corresponding standard exception.
  381. ///
  382. /// Note that this function returns a bare pointer to the newly allocated
  383. /// object, not a shared pointer object enclosing the pointer.
  384. /// The caller is responsible for deleting the object after the use of it
  385. /// (typically, the caller would immediately encapsulate the returned pointer
  386. /// in a shared pointer object, \c EDNSPtr or \c ConstEDNSPtr).
  387. /// It returns a bare pointer so that it can be used where the use of a shared
  388. /// pointer is impossible or not desirable.
  389. ///
  390. /// Note to developers: there is no strong technical reason why this function
  391. /// cannot be a constructor of the \c EDNS class or even integrated into the
  392. /// constructor. But we decided to make it a separate free function so that
  393. /// constructors will be free from side effects (which is in itself a matter
  394. /// of preference).
  395. ///
  396. /// \param name The owner name of the OPT RR. This must be the root name.
  397. /// \param rrclass The RR class of the OPT RR.
  398. /// \param rrtype This must specify the OPT RR type.
  399. /// \param rrttl The TTL of the OPT RR.
  400. /// \param rdata The RDATA of the OPT RR.
  401. /// \param extended_rcode A placeholder to store the topmost 8 bits of the
  402. /// extended Rcode.
  403. /// \return A pointer to the created \c EDNS object.
  404. EDNS* createEDNSFromRR(const Name& name, const RRClass& rrclass,
  405. const RRType& rrtype, const RRTTL& ttl,
  406. const rdata::Rdata& rdata, uint8_t& extended_rcode);
  407. /// \brief Insert the \c EDNS as a string into stream.
  408. ///
  409. /// This method convert \c edns into a string and inserts it into the
  410. /// output stream \c os.
  411. ///
  412. /// \param os A \c std::ostream object on which the insertion operation is
  413. /// performed.
  414. /// \param edns A reference to an \c EDNS object output by the operation.
  415. /// \return A reference to the same \c std::ostream object referenced by
  416. /// parameter \c os after the insertion operation.
  417. std::ostream& operator<<(std::ostream& os, const EDNS& edns);
  418. }
  419. }
  420. #endif // __EDNS_H
  421. // Local Variables:
  422. // mode: c++
  423. // End: