option.cc 11 KB

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