option6_client_fqdn.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // Copyright (C) 2013 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 <dhcp/dhcp6.h>
  15. #include <dhcp/option6_client_fqdn.h>
  16. #include <dns/labelsequence.h>
  17. #include <util/buffer.h>
  18. #include <util/io_utilities.h>
  19. #include <util/strutil.h>
  20. #include <sstream>
  21. namespace isc {
  22. namespace dhcp {
  23. /// @brief Implements the logic for the Option6ClientFqdn class.
  24. ///
  25. /// The purpose of the class is to separate the implementation details
  26. /// of the Option6ClientFqdn class from the interface. This implementation
  27. /// uses b10-libdns classes to process FQDNs. At some point it may be
  28. /// desired to split b10-libdhcp++ from b10-libdns. In such case the
  29. /// implementation of this class may be changed. The declaration of the
  30. /// Option6ClientFqdn class holds the pointer to implementation, so
  31. /// the transition to a different implementation would not affect the
  32. /// header file.
  33. class Option6ClientFqdnImpl {
  34. public:
  35. /// Holds flags carried by the option.
  36. uint8_t flags_;
  37. /// Holds the pointer to a domain name carried in the option.
  38. boost::shared_ptr<isc::dns::Name> domain_name_;
  39. /// Indicates whether domain name is partial or fully qualified.
  40. Option6ClientFqdn::DomainNameType domain_name_type_;
  41. /// @brief Constructor, from domain name.
  42. ///
  43. /// @param flags A value of the flags option field.
  44. /// @param domain_name A domain name carried by the option given in the
  45. /// textual format.
  46. /// @param domain_name_type A value which indicates whether domain-name
  47. /// is partial of fully qualified.
  48. Option6ClientFqdnImpl(const uint8_t flags,
  49. const std::string& domain_name,
  50. const Option6ClientFqdn::DomainNameType name_type);
  51. /// @brief Constructor, from wire data.
  52. ///
  53. /// @param first An iterator pointing to the begining of the option data
  54. /// in the wire format.
  55. /// @param last An iterator poiting to the end of the option data in the
  56. /// wire format.
  57. Option6ClientFqdnImpl(OptionBufferConstIter first,
  58. OptionBufferConstIter last);
  59. /// @brief Copy constructor.
  60. ///
  61. /// @param source An object being copied.
  62. Option6ClientFqdnImpl(const Option6ClientFqdnImpl& source);
  63. /// @brief Assignment operator.
  64. ///
  65. /// @param source An object which is being assigned.
  66. Option6ClientFqdnImpl& operator=(const Option6ClientFqdnImpl& source);
  67. /// @brief Set a new domain name for the option.
  68. ///
  69. /// @param domain_name A new domain name to be assigned.
  70. /// @param name_type A value which indicates whether the domain-name is
  71. /// partial or fully qualified.
  72. void setDomainName(const std::string& domain_name,
  73. const Option6ClientFqdn::DomainNameType name_type);
  74. /// @brief Check if flags are valid.
  75. ///
  76. /// In particular, this function checks if the N and S bits are not
  77. /// set to 1 in the same time.
  78. ///
  79. /// @param flags A value carried by the flags field of the option.
  80. /// @param check_mbz A boolean value which indicates if this function should
  81. /// check if the MBZ bits are set (if true). This parameter should be set
  82. /// to false when validating flags in the received message. This is because
  83. /// server should ignore MBZ bits in received messages.
  84. /// @throw InvalidFqdnOptionFlags if flags are invalid.
  85. static void checkFlags(const uint8_t flags, const bool check_mbz);
  86. /// @brief Parse the Option provided in the wire format.
  87. ///
  88. /// @param first An iterator pointing to the begining of the option data
  89. /// in the wire format.
  90. /// @param last An iterator poiting to the end of the option data in the
  91. /// wire format.
  92. void parseWireData(OptionBufferConstIter first,
  93. OptionBufferConstIter last);
  94. };
  95. Option6ClientFqdnImpl::
  96. Option6ClientFqdnImpl(const uint8_t flags,
  97. const std::string& domain_name,
  98. const Option6ClientFqdn::DomainNameType name_type)
  99. : flags_(flags),
  100. domain_name_(),
  101. domain_name_type_(name_type) {
  102. // Check if flags are correct. Also check if MBZ bits are set.
  103. checkFlags(flags_, true);
  104. // Set domain name. It may throw an exception if domain name has wrong
  105. // format.
  106. setDomainName(domain_name, name_type);
  107. }
  108. Option6ClientFqdnImpl::Option6ClientFqdnImpl(OptionBufferConstIter first,
  109. OptionBufferConstIter last) {
  110. parseWireData(first, last);
  111. // Verify that flags value was correct. Do not check if MBZ bits are
  112. // set because we should ignore those bits in received message.
  113. checkFlags(flags_, false);
  114. }
  115. Option6ClientFqdnImpl::
  116. Option6ClientFqdnImpl(const Option6ClientFqdnImpl& source)
  117. : flags_(source.flags_),
  118. domain_name_(),
  119. domain_name_type_(source.domain_name_type_) {
  120. if (source.domain_name_) {
  121. domain_name_.reset(new isc::dns::Name(*source.domain_name_));
  122. }
  123. }
  124. Option6ClientFqdnImpl&
  125. Option6ClientFqdnImpl::operator=(const Option6ClientFqdnImpl& source) {
  126. domain_name_.reset(new isc::dns::Name(*source.domain_name_));
  127. // This assignment should be exception safe.
  128. flags_ = source.flags_;
  129. domain_name_type_ = source.domain_name_type_;
  130. return (*this);
  131. }
  132. void
  133. Option6ClientFqdnImpl::
  134. setDomainName(const std::string& domain_name,
  135. const Option6ClientFqdn::DomainNameType name_type) {
  136. // domain-name must be trimmed. Otherwise, string comprising spaces only
  137. // would be treated as a fully qualified name.
  138. std::string name = isc::util::str::trim(domain_name);
  139. if (name.empty()) {
  140. if (name_type == Option6ClientFqdn::FULL) {
  141. isc_throw(InvalidFqdnOptionDomainName,
  142. "fully qualified domain-name must not be empty"
  143. << " when setting new domain-name for DHCPv6 Client"
  144. << " FQDN Option");
  145. }
  146. // The special case when domain-name is empty is marked by setting the
  147. // pointer to the domain-name object to NULL.
  148. domain_name_.reset();
  149. } else {
  150. try {
  151. domain_name_.reset(new isc::dns::Name(name));
  152. domain_name_type_ = name_type;
  153. } catch (const Exception& ex) {
  154. isc_throw(InvalidFqdnOptionDomainName, "invalid domain-name value '"
  155. << domain_name << "' when setting new domain-name for"
  156. << " DHCPv6 Client FQDN Option");
  157. }
  158. }
  159. }
  160. void
  161. Option6ClientFqdnImpl::checkFlags(const uint8_t flags, const bool check_mbz) {
  162. // The Must Be Zero (MBZ) bits must not be set.
  163. if (check_mbz && ((flags & ~Option6ClientFqdn::FLAG_MASK) != 0)) {
  164. isc_throw(InvalidFqdnOptionFlags,
  165. "invalid DHCPv6 Client FQDN Option flags: 0x"
  166. << std::hex << static_cast<int>(flags) << std::dec);
  167. }
  168. // According to RFC 4704, section 4.1. if the N bit is 1, the S bit
  169. // MUST be 0. Checking it here.
  170. if ((flags & (Option6ClientFqdn::FLAG_N | Option6ClientFqdn::FLAG_S))
  171. == (Option6ClientFqdn::FLAG_N | Option6ClientFqdn::FLAG_S)) {
  172. isc_throw(InvalidFqdnOptionFlags,
  173. "both N and S flag of the DHCPv6 Client FQDN Option are set."
  174. << " According to RFC 4704, if the N bit is 1 the S bit"
  175. << " MUST be 0");
  176. }
  177. }
  178. void
  179. Option6ClientFqdnImpl::parseWireData(OptionBufferConstIter first,
  180. OptionBufferConstIter last) {
  181. // Buffer must comprise at least one byte with the flags.
  182. // The domain-name may be empty.
  183. if (std::distance(first, last) < Option6ClientFqdn::FLAG_FIELD_LEN) {
  184. isc_throw(OutOfRange, "DHCPv6 Client FQDN Option ("
  185. << D6O_CLIENT_FQDN << ") is truncated. Minimal option"
  186. << " size is " << Option6ClientFqdn::FLAG_FIELD_LEN
  187. << ", got option with size " << std::distance(first, last));
  188. }
  189. // Parse flags
  190. flags_ = *(first++);
  191. // Parse domain-name if any.
  192. if (std::distance(first, last) > 0) {
  193. // The FQDN may comprise a partial domain-name. In this case it lacks
  194. // terminating 0. If this is the case, we will need to add zero at
  195. // the end because Name object constructor requires it.
  196. if (*(last - 1) != 0) {
  197. // Create temporary buffer and add terminating zero.
  198. OptionBuffer buf(first, last);
  199. buf.push_back(0);
  200. // Reset domain name.
  201. isc::util::InputBuffer name_buf(&buf[0], buf.size());
  202. domain_name_.reset(new isc::dns::Name(name_buf));
  203. // Terminating zero was missing, so set the domain-name type
  204. // to partial.
  205. domain_name_type_ = Option6ClientFqdn::PARTIAL;
  206. } else {
  207. // We are dealing with fully qualified domain name so there is
  208. // no need to add terminating zero. Simply pass the buffer to
  209. // Name object constructor.
  210. isc::util::InputBuffer name_buf(&(*first),
  211. std::distance(first, last));
  212. domain_name_.reset(new isc::dns::Name(name_buf));
  213. // Set the domain-type to fully qualified domain name.
  214. domain_name_type_ = Option6ClientFqdn::FULL;
  215. }
  216. }
  217. }
  218. Option6ClientFqdn::Option6ClientFqdn(const uint8_t flag)
  219. : Option(Option::V6, D6O_CLIENT_FQDN),
  220. impl_(new Option6ClientFqdnImpl(flag, "", PARTIAL)) {
  221. }
  222. Option6ClientFqdn::Option6ClientFqdn(const uint8_t flag,
  223. const std::string& domain_name,
  224. const DomainNameType domain_name_type)
  225. : Option(Option::V6, D6O_CLIENT_FQDN),
  226. impl_(new Option6ClientFqdnImpl(flag, domain_name, domain_name_type)) {
  227. }
  228. Option6ClientFqdn::Option6ClientFqdn(OptionBufferConstIter first,
  229. OptionBufferConstIter last)
  230. : Option(Option::V6, D6O_CLIENT_FQDN, first, last),
  231. impl_(new Option6ClientFqdnImpl(first, last)) {
  232. }
  233. Option6ClientFqdn::~Option6ClientFqdn() {
  234. delete(impl_);
  235. }
  236. Option6ClientFqdn::Option6ClientFqdn(const Option6ClientFqdn& source)
  237. : Option(source),
  238. impl_(new Option6ClientFqdnImpl(*source.impl_)) {
  239. }
  240. Option6ClientFqdn&
  241. Option6ClientFqdn::operator=(const Option6ClientFqdn& source) {
  242. Option6ClientFqdnImpl* old_impl = impl_;
  243. impl_ = new Option6ClientFqdnImpl(*source.impl_);
  244. delete(old_impl);
  245. return (*this);
  246. }
  247. bool
  248. Option6ClientFqdn::getFlag(const Flag flag) const {
  249. // Caller should query for one of the: N, S or O flags. However, enumerator
  250. // value of 0x3 is valid (because it belongs to the range between the
  251. // lowest and highest enumerator). The value 0x3 represents two flags:
  252. // S and O and would cause ambiguity. Therefore, we selectively check
  253. // that the flag is equal to one of the explicit enumerator values. If
  254. // not, throw an exception.
  255. if (flag != FLAG_S && flag != FLAG_O && flag != FLAG_N) {
  256. isc_throw(InvalidFqdnOptionFlags, "invalid DHCPv6 Client FQDN"
  257. << " Option flag specified, expected N, S or O");
  258. }
  259. return ((impl_->flags_ & flag) != 0);
  260. }
  261. void
  262. Option6ClientFqdn::setFlag(const Flag flag, const bool set_flag) {
  263. // Check that flag is in range between 0x1 and 0x7. Note that this
  264. // allows to set or clear multiple flags concurrently.
  265. if (((flag & ~FLAG_MASK) != 0) || (flag == 0)) {
  266. isc_throw(InvalidFqdnOptionFlags, "invalid DHCPv6 Client FQDN"
  267. << " Option flag " << std::hex
  268. << static_cast<int>(flag) << std::dec
  269. << "is being set. Expected combination of N, S and O");
  270. }
  271. // Copy the current flags into local variable. That way we will be able
  272. // to test new flags settings before applying them.
  273. uint8_t new_flag = impl_->flags_;
  274. if (set_flag) {
  275. new_flag |= flag;
  276. } else {
  277. new_flag &= ~flag;
  278. }
  279. // Check new flags. If they are valid, apply them.
  280. Option6ClientFqdnImpl::checkFlags(new_flag, true);
  281. impl_->flags_ = new_flag;
  282. }
  283. void
  284. Option6ClientFqdn::resetFlags() {
  285. impl_->flags_ = 0;
  286. }
  287. std::string
  288. Option6ClientFqdn::getDomainName() const {
  289. if (impl_->domain_name_) {
  290. return (impl_->domain_name_->toText(impl_->domain_name_type_ ==
  291. PARTIAL));
  292. }
  293. // If an object holding domain-name is NULL it means that the domain-name
  294. // is empty.
  295. return ("");
  296. }
  297. void
  298. Option6ClientFqdn::packDomainName(isc::util::OutputBuffer& buf) const {
  299. // Domain name, encoded as a set of labels.
  300. isc::dns::LabelSequence labels(*impl_->domain_name_);
  301. if (labels.getDataLength() > 0) {
  302. size_t read_len = 0;
  303. const uint8_t* data = labels.getData(&read_len);
  304. if (impl_->domain_name_type_ == PARTIAL) {
  305. --read_len;
  306. }
  307. buf.writeData(data, read_len);
  308. }
  309. }
  310. void
  311. Option6ClientFqdn::setDomainName(const std::string& domain_name,
  312. const DomainNameType domain_name_type) {
  313. impl_->setDomainName(domain_name, domain_name_type);
  314. }
  315. void
  316. Option6ClientFqdn::resetDomainName() {
  317. setDomainName("", PARTIAL);
  318. }
  319. Option6ClientFqdn::DomainNameType
  320. Option6ClientFqdn::getDomainNameType() const {
  321. return (impl_->domain_name_type_);
  322. }
  323. void
  324. Option6ClientFqdn::pack(isc::util::OutputBuffer& buf) {
  325. // Header = option code and length.
  326. packHeader(buf);
  327. // Flags field.
  328. buf.writeUint8(impl_->flags_);
  329. // Domain name.
  330. packDomainName(buf);
  331. }
  332. void
  333. Option6ClientFqdn::unpack(OptionBufferConstIter first,
  334. OptionBufferConstIter last) {
  335. setData(first, last);
  336. impl_->parseWireData(first, last);
  337. }
  338. std::string
  339. Option6ClientFqdn::toText(int indent) {
  340. std::ostringstream stream;
  341. std::string in(indent, ' '); // base indentation
  342. std::string in_add(2, ' '); // second-level indentation is 2 spaces long
  343. stream << in << "type=" << type_ << "(CLIENT_FQDN)" << std::endl
  344. << in << "flags:" << std::endl
  345. << in << in_add << "N=" << (getFlag(FLAG_N) ? "1" : "0") << std::endl
  346. << in << in_add << "O=" << (getFlag(FLAG_O) ? "1" : "0") << std::endl
  347. << in << in_add << "S=" << (getFlag(FLAG_S) ? "1" : "0") << std::endl
  348. << in << "domain-name='" << getDomainName() << "' ("
  349. << (getDomainNameType() == PARTIAL ? "partial" : "full")
  350. << ")" << std::endl;
  351. return (stream.str());
  352. }
  353. uint16_t
  354. Option6ClientFqdn::len() {
  355. // If domain name is partial, the NULL terminating character
  356. // is not included and the option length have to be adjusted.
  357. uint16_t domain_name_length = impl_->domain_name_type_ == FULL ?
  358. impl_->domain_name_->getLength() : impl_->domain_name_->getLength() - 1;
  359. return (getHeaderLen() + FLAG_FIELD_LEN + domain_name_length);
  360. }
  361. } // end of isc::dhcp namespace
  362. } // end of isc namespace