option.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // Copyright (C) 2011-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <dhcp/libdhcp++.h>
  8. #include <dhcp/option.h>
  9. #include <exceptions/exceptions.h>
  10. #include <util/encode/hex.h>
  11. #include <util/io_utilities.h>
  12. #include <iomanip>
  13. #include <list>
  14. #include <sstream>
  15. #include <arpa/inet.h>
  16. #include <stdint.h>
  17. #include <string.h>
  18. using namespace std;
  19. using namespace isc::util;
  20. namespace isc {
  21. namespace dhcp {
  22. OptionPtr
  23. Option::factory(Option::Universe u,
  24. uint16_t type,
  25. const OptionBuffer& buf) {
  26. return(LibDHCP::optionFactory(u, type, buf));
  27. }
  28. Option::Option(Universe u, uint16_t type)
  29. :universe_(u), type_(type) {
  30. // END option (type 255 is forbidden as well)
  31. if ((u == V4) && ((type == 0) || (type > 254))) {
  32. isc_throw(BadValue, "Can't create V4 option of type "
  33. << type << ", V4 options are in range 1..254");
  34. }
  35. }
  36. Option::Option(Universe u, uint16_t type, const OptionBuffer& data)
  37. :universe_(u), type_(type), data_(data) {
  38. check();
  39. }
  40. Option::Option(Universe u, uint16_t type, OptionBufferConstIter first,
  41. OptionBufferConstIter last)
  42. :universe_(u), type_(type), data_(first, last) {
  43. check();
  44. }
  45. Option::Option(const Option& option)
  46. : universe_(option.universe_), type_(option.type_),
  47. data_(option.data_), options_(),
  48. encapsulated_space_(option.encapsulated_space_) {
  49. option.getOptionsCopy(options_);
  50. }
  51. Option&
  52. Option::operator=(const Option& rhs) {
  53. if (&rhs != this) {
  54. universe_ = rhs.universe_;
  55. type_ = rhs.type_;
  56. data_ = rhs.data_;
  57. rhs.getOptionsCopy(options_);
  58. encapsulated_space_ = rhs.encapsulated_space_;
  59. }
  60. return (*this);
  61. }
  62. OptionPtr
  63. Option::clone() const {
  64. return (cloneInternal<Option>());
  65. }
  66. void
  67. Option::check() const {
  68. if ( (universe_ != V4) && (universe_ != V6) ) {
  69. isc_throw(BadValue, "Invalid universe type specified. "
  70. << "Only V4 and V6 are allowed.");
  71. }
  72. if (universe_ == V4) {
  73. if (type_ > 255) {
  74. isc_throw(OutOfRange, "DHCPv4 Option type " << type_ << " is too big. "
  75. << "For DHCPv4 allowed type range is 0..255");
  76. } else if (data_.size() > 255) {
  77. isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big.");
  78. /// TODO Larger options can be stored as separate instances
  79. /// of DHCPv4 options. Clients MUST concatenate them.
  80. /// Fortunately, there are no such large options used today.
  81. }
  82. }
  83. // no need to check anything for DHCPv6. It allows full range (0-64k) of
  84. // both types and data size.
  85. }
  86. void Option::pack(isc::util::OutputBuffer& buf) const {
  87. // Write a header.
  88. packHeader(buf);
  89. // Write data.
  90. if (!data_.empty()) {
  91. buf.writeData(&data_[0], data_.size());
  92. }
  93. // Write sub-options.
  94. packOptions(buf);
  95. }
  96. void
  97. Option::packHeader(isc::util::OutputBuffer& buf) const {
  98. if (universe_ == V4) {
  99. if (len() > 255) {
  100. isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big. "
  101. << "At most 255 bytes are supported.");
  102. /// TODO Larger options can be stored as separate instances
  103. /// of DHCPv4 options. Clients MUST concatenate them.
  104. /// Fortunately, there are no such large options used today.
  105. }
  106. buf.writeUint8(type_);
  107. buf.writeUint8(len() - getHeaderLen());
  108. } else {
  109. buf.writeUint16(type_);
  110. buf.writeUint16(len() - getHeaderLen());
  111. }
  112. }
  113. void
  114. Option::packOptions(isc::util::OutputBuffer& buf) const {
  115. switch (universe_) {
  116. case V4:
  117. LibDHCP::packOptions4(buf, options_);
  118. return;
  119. case V6:
  120. LibDHCP::packOptions6(buf, options_);
  121. return;
  122. default:
  123. isc_throw(isc::BadValue, "Invalid universe type " << universe_);
  124. }
  125. }
  126. void Option::unpack(OptionBufferConstIter begin,
  127. OptionBufferConstIter end) {
  128. setData(begin, end);
  129. }
  130. void
  131. Option::unpackOptions(const OptionBuffer& buf) {
  132. list<uint16_t> deferred;
  133. switch (universe_) {
  134. case V4:
  135. LibDHCP::unpackOptions4(buf, getEncapsulatedSpace(),
  136. options_, deferred);
  137. return;
  138. case V6:
  139. LibDHCP::unpackOptions6(buf, getEncapsulatedSpace(), options_);
  140. return;
  141. default:
  142. isc_throw(isc::BadValue, "Invalid universe type " << universe_);
  143. }
  144. }
  145. uint16_t Option::len() const {
  146. // Returns length of the complete option (data length + DHCPv4/DHCPv6
  147. // option header)
  148. // length of the whole option is header and data stored in this option...
  149. size_t length = getHeaderLen() + data_.size();
  150. // ... and sum of lengths of all suboptions
  151. for (OptionCollection::const_iterator it = options_.begin();
  152. it != options_.end();
  153. ++it) {
  154. length += (*it).second->len();
  155. }
  156. // note that this is not equal to length field. This value denotes
  157. // number of bytes required to store this option. length option should
  158. // contain (len()-getHeaderLen()) value.
  159. return (static_cast<uint16_t>(length));
  160. }
  161. bool
  162. Option::valid() const {
  163. if (universe_ != V4 &&
  164. universe_ != V6) {
  165. return (false);
  166. }
  167. return (true);
  168. }
  169. OptionPtr Option::getOption(uint16_t opt_type) const {
  170. isc::dhcp::OptionCollection::const_iterator x =
  171. options_.find(opt_type);
  172. if ( x != options_.end() ) {
  173. return (*x).second;
  174. }
  175. return OptionPtr(); // NULL
  176. }
  177. void
  178. Option::getOptionsCopy(OptionCollection& options_copy) const {
  179. OptionCollection local_options;
  180. for (OptionCollection::const_iterator it = options_.begin();
  181. it != options_.end(); ++it) {
  182. OptionPtr copy = it->second->clone();
  183. local_options.insert(std::make_pair(it->second->getType(),
  184. copy));
  185. }
  186. // All options copied successfully, so assign them to the output
  187. // parameter.
  188. options_copy.swap(local_options);
  189. }
  190. bool Option::delOption(uint16_t opt_type) {
  191. isc::dhcp::OptionCollection::iterator x = options_.find(opt_type);
  192. if ( x != options_.end() ) {
  193. options_.erase(x);
  194. return true; // delete successful
  195. }
  196. return (false); // option not found, can't delete
  197. }
  198. std::string Option::toText(int indent) const {
  199. std::stringstream output;
  200. output << headerToText(indent) << ": ";
  201. for (unsigned int i = 0; i < data_.size(); i++) {
  202. if (i) {
  203. output << ":";
  204. }
  205. output << setfill('0') << setw(2) << hex
  206. << static_cast<unsigned short>(data_[i]);
  207. }
  208. // Append suboptions.
  209. output << suboptionsToText(indent + 2);
  210. return (output.str());
  211. }
  212. std::string
  213. Option::toString() const {
  214. /// @todo: Implement actual conversion in derived classes.
  215. return (toText(0));
  216. }
  217. std::vector<uint8_t>
  218. Option::toBinary(const bool include_header) const {
  219. OutputBuffer buf(len());
  220. try {
  221. // If the option is too long, exception will be thrown. We allow
  222. // for this exception to propagate to not mask this error.
  223. pack(buf);
  224. } catch (const std::exception &ex) {
  225. isc_throw(OutOfRange, "unable to obtain hexadecimal representation"
  226. " of option " << getType() << ": " << ex.what());
  227. }
  228. const uint8_t* option_data = static_cast<const uint8_t*>(buf.getData());
  229. // Assign option data to a vector, with or without option header depending
  230. // on the value of "include_header" flag.
  231. std::vector<uint8_t> option_vec(option_data + (include_header ? 0 : getHeaderLen()),
  232. option_data + buf.getLength());
  233. return (option_vec);
  234. }
  235. std::string
  236. Option::toHexString(const bool include_header) const {
  237. // Prepare binary version of the option.
  238. std::vector<uint8_t> option_vec = toBinary(include_header);
  239. // Return hexadecimal representation prepended with 0x or empty string
  240. // if option has no payload and the header fields are excluded.
  241. std::ostringstream s;
  242. if (!option_vec.empty()) {
  243. s << "0x" << encode::encodeHex(option_vec);
  244. }
  245. return (s.str());
  246. }
  247. std::string
  248. Option::headerToText(const int indent, const std::string& type_name) const {
  249. std::stringstream output;
  250. for (int i = 0; i < indent; i++)
  251. output << " ";
  252. int field_len = (getUniverse() == V4 ? 3 : 5);
  253. output << "type=" << std::setw(field_len) << std::setfill('0')
  254. << type_;
  255. if (!type_name.empty()) {
  256. output << "(" << type_name << ")";
  257. }
  258. output << ", len=" << std::setw(field_len) << std::setfill('0')
  259. << len()-getHeaderLen();
  260. return (output.str());
  261. }
  262. std::string
  263. Option::suboptionsToText(const int indent) const {
  264. std::stringstream output;
  265. if (!options_.empty()) {
  266. output << "," << std::endl << "options:";
  267. for (OptionCollection::const_iterator opt = options_.begin();
  268. opt != options_.end(); ++opt) {
  269. output << std::endl << (*opt).second->toText(indent);
  270. }
  271. }
  272. return (output.str());
  273. }
  274. uint16_t
  275. Option::getHeaderLen() const {
  276. switch (universe_) {
  277. case V4:
  278. return OPTION4_HDR_LEN; // header length for v4
  279. case V6:
  280. return OPTION6_HDR_LEN; // header length for v6
  281. }
  282. return 0; // should not happen
  283. }
  284. void Option::addOption(OptionPtr opt) {
  285. if (universe_ == V4) {
  286. // check for uniqueness (DHCPv4 options must be unique)
  287. if (getOption(opt->getType())) {
  288. isc_throw(BadValue, "Option " << opt->getType()
  289. << " already present in this message.");
  290. }
  291. }
  292. options_.insert(make_pair(opt->getType(), opt));
  293. }
  294. uint8_t Option::getUint8() const {
  295. if (data_.size() < sizeof(uint8_t) ) {
  296. isc_throw(OutOfRange, "Attempt to read uint8 from option " << type_
  297. << " that has size " << data_.size());
  298. }
  299. return (data_[0]);
  300. }
  301. uint16_t Option::getUint16() const {
  302. // readUint16() checks and throws OutOfRange if data_ is too small.
  303. return (readUint16(&data_[0], data_.size()));
  304. }
  305. uint32_t Option::getUint32() const {
  306. // readUint32() checks and throws OutOfRange if data_ is too small.
  307. return (readUint32(&data_[0], data_.size()));
  308. }
  309. void Option::setUint8(uint8_t value) {
  310. data_.resize(sizeof(value));
  311. data_[0] = value;
  312. }
  313. void Option::setUint16(uint16_t value) {
  314. data_.resize(sizeof(value));
  315. writeUint16(value, &data_[0], data_.size());
  316. }
  317. void Option::setUint32(uint32_t value) {
  318. data_.resize(sizeof(value));
  319. writeUint32(value, &data_[0], data_.size());
  320. }
  321. bool Option::equals(const OptionPtr& other) const {
  322. return (equals(*other));
  323. }
  324. bool Option::equals(const Option& other) const {
  325. return ( (getType() == other.getType()) &&
  326. (getData() == other.getData()) );
  327. }
  328. Option::~Option() {
  329. }
  330. } // end of isc::dhcp namespace
  331. } // end of isc namespace