option.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // Copyright (C) 2011-2013, 2015 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 <config.h>
  15. #include <dhcp/libdhcp++.h>
  16. #include <dhcp/option.h>
  17. #include <exceptions/exceptions.h>
  18. #include <util/io_utilities.h>
  19. #include <iomanip>
  20. #include <sstream>
  21. #include <arpa/inet.h>
  22. #include <stdint.h>
  23. #include <string.h>
  24. using namespace std;
  25. using namespace isc::util;
  26. namespace isc {
  27. namespace dhcp {
  28. OptionPtr
  29. Option::factory(Option::Universe u,
  30. uint16_t type,
  31. const OptionBuffer& buf) {
  32. return(LibDHCP::optionFactory(u, type, buf));
  33. }
  34. Option::Option(Universe u, uint16_t type)
  35. :universe_(u), type_(type) {
  36. // END option (type 255 is forbidden as well)
  37. if ((u == V4) && ((type == 0) || (type > 254))) {
  38. isc_throw(BadValue, "Can't create V4 option of type "
  39. << type << ", V4 options are in range 1..254");
  40. }
  41. }
  42. Option::Option(Universe u, uint16_t type, const OptionBuffer& data)
  43. :universe_(u), type_(type), data_(data) {
  44. check();
  45. }
  46. Option::Option(Universe u, uint16_t type, OptionBufferConstIter first,
  47. OptionBufferConstIter last)
  48. :universe_(u), type_(type), data_(first, last) {
  49. check();
  50. }
  51. void
  52. Option::check() {
  53. if ( (universe_ != V4) && (universe_ != V6) ) {
  54. isc_throw(BadValue, "Invalid universe type specified. "
  55. << "Only V4 and V6 are allowed.");
  56. }
  57. if (universe_ == V4) {
  58. if (type_ > 255) {
  59. isc_throw(OutOfRange, "DHCPv4 Option type " << type_ << " is too big. "
  60. << "For DHCPv4 allowed type range is 0..255");
  61. } else if (data_.size() > 255) {
  62. isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big.");
  63. /// TODO Larger options can be stored as separate instances
  64. /// of DHCPv4 options. Clients MUST concatenate them.
  65. /// Fortunately, there are no such large options used today.
  66. }
  67. }
  68. // no need to check anything for DHCPv6. It allows full range (0-64k) of
  69. // both types and data size.
  70. }
  71. void Option::pack(isc::util::OutputBuffer& buf) {
  72. // Write a header.
  73. packHeader(buf);
  74. // Write data.
  75. if (!data_.empty()) {
  76. buf.writeData(&data_[0], data_.size());
  77. }
  78. // Write sub-options.
  79. packOptions(buf);
  80. }
  81. void
  82. Option::packHeader(isc::util::OutputBuffer& buf) {
  83. if (universe_ == V4) {
  84. if (len() > 255) {
  85. isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big. "
  86. << "At most 255 bytes are supported.");
  87. /// TODO Larger options can be stored as separate instances
  88. /// of DHCPv4 options. Clients MUST concatenate them.
  89. /// Fortunately, there are no such large options used today.
  90. }
  91. buf.writeUint8(type_);
  92. buf.writeUint8(len() - getHeaderLen());
  93. } else {
  94. buf.writeUint16(type_);
  95. buf.writeUint16(len() - getHeaderLen());
  96. }
  97. }
  98. void
  99. Option::packOptions(isc::util::OutputBuffer& buf) {
  100. LibDHCP::packOptions(buf, options_);
  101. }
  102. void Option::unpack(OptionBufferConstIter begin,
  103. OptionBufferConstIter end) {
  104. setData(begin, end);
  105. }
  106. void
  107. Option::unpackOptions(const OptionBuffer& buf) {
  108. // If custom option parsing function has been set, use this function
  109. // to parse options. Otherwise, use standard function from libdhcp++.
  110. if (!callback_.empty()) {
  111. callback_(buf, getEncapsulatedSpace(), options_, 0, 0);
  112. return;
  113. }
  114. switch (universe_) {
  115. case V4:
  116. LibDHCP::unpackOptions4(buf, getEncapsulatedSpace(), options_);
  117. return;
  118. case V6:
  119. LibDHCP::unpackOptions6(buf, getEncapsulatedSpace(), options_);
  120. return;
  121. default:
  122. isc_throw(isc::BadValue, "Invalid universe type " << universe_);
  123. }
  124. }
  125. uint16_t Option::len() {
  126. // Returns length of the complete option (data length + DHCPv4/DHCPv6
  127. // option header)
  128. // length of the whole option is header and data stored in this option...
  129. int length = getHeaderLen() + data_.size();
  130. // ... and sum of lengths of all suboptions
  131. for (OptionCollection::iterator it = options_.begin();
  132. it != options_.end();
  133. ++it) {
  134. length += (*it).second->len();
  135. }
  136. // note that this is not equal to lenght field. This value denotes
  137. // number of bytes required to store this option. length option should
  138. // contain (len()-getHeaderLen()) value.
  139. return (length);
  140. }
  141. bool
  142. Option::valid() {
  143. if (universe_ != V4 &&
  144. universe_ != V6) {
  145. return (false);
  146. }
  147. return (true);
  148. }
  149. OptionPtr Option::getOption(uint16_t opt_type) {
  150. isc::dhcp::OptionCollection::const_iterator x =
  151. options_.find(opt_type);
  152. if ( x != options_.end() ) {
  153. return (*x).second;
  154. }
  155. return OptionPtr(); // NULL
  156. }
  157. bool Option::delOption(uint16_t opt_type) {
  158. isc::dhcp::OptionCollection::iterator x = options_.find(opt_type);
  159. if ( x != options_.end() ) {
  160. options_.erase(x);
  161. return true; // delete successful
  162. }
  163. return (false); // option not found, can't delete
  164. }
  165. std::string Option::toText(int indent) {
  166. std::stringstream output;
  167. output << headerToText(indent) << ": ";
  168. for (unsigned int i = 0; i < data_.size(); i++) {
  169. if (i) {
  170. output << ":";
  171. }
  172. output << setfill('0') << setw(2) << hex
  173. << static_cast<unsigned short>(data_[i]);
  174. }
  175. // Append suboptions.
  176. output << suboptionsToText(indent + 2);
  177. return (output.str());
  178. }
  179. std::string
  180. Option::headerToText(const int indent, const std::string& type_name) {
  181. std::stringstream output;
  182. for (int i = 0; i < indent; i++)
  183. output << " ";
  184. int field_len = (getUniverse() == V4 ? 3 : 5);
  185. output << "type=" << std::setw(field_len) << std::setfill('0')
  186. << type_;
  187. if (!type_name.empty()) {
  188. output << "(" << type_name << ")";
  189. }
  190. output << ", len=" << std::setw(field_len) << std::setfill('0')
  191. << len()-getHeaderLen();
  192. return (output.str());
  193. }
  194. std::string
  195. Option::suboptionsToText(const int indent) const {
  196. std::stringstream output;
  197. if (!options_.empty()) {
  198. output << "," << std::endl << "options:";
  199. for (OptionCollection::const_iterator opt = options_.begin();
  200. opt != options_.end(); ++opt) {
  201. output << std::endl << (*opt).second->toText(indent);
  202. }
  203. }
  204. return (output.str());
  205. }
  206. uint16_t
  207. Option::getHeaderLen() {
  208. switch (universe_) {
  209. case V4:
  210. return OPTION4_HDR_LEN; // header length for v4
  211. case V6:
  212. return OPTION6_HDR_LEN; // header length for v6
  213. }
  214. return 0; // should not happen
  215. }
  216. void Option::addOption(OptionPtr opt) {
  217. if (universe_ == V4) {
  218. // check for uniqueness (DHCPv4 options must be unique)
  219. if (getOption(opt->getType())) {
  220. isc_throw(BadValue, "Option " << opt->getType()
  221. << " already present in this message.");
  222. }
  223. }
  224. options_.insert(make_pair(opt->getType(), opt));
  225. }
  226. uint8_t Option::getUint8() {
  227. if (data_.size() < sizeof(uint8_t) ) {
  228. isc_throw(OutOfRange, "Attempt to read uint8 from option " << type_
  229. << " that has size " << data_.size());
  230. }
  231. return (data_[0]);
  232. }
  233. uint16_t Option::getUint16() {
  234. // readUint16() checks and throws OutOfRange if data_ is too small.
  235. return (readUint16(&data_[0], data_.size()));
  236. }
  237. uint32_t Option::getUint32() {
  238. // readUint32() checks and throws OutOfRange if data_ is too small.
  239. return (readUint32(&data_[0], data_.size()));
  240. }
  241. void Option::setUint8(uint8_t value) {
  242. data_.resize(sizeof(value));
  243. data_[0] = value;
  244. }
  245. void Option::setUint16(uint16_t value) {
  246. data_.resize(sizeof(value));
  247. writeUint16(value, &data_[0], data_.size());
  248. }
  249. void Option::setUint32(uint32_t value) {
  250. data_.resize(sizeof(value));
  251. writeUint32(value, &data_[0], data_.size());
  252. }
  253. bool Option::equals(const OptionPtr& other) const {
  254. return (equals(*other));
  255. }
  256. bool Option::equals(const Option& other) const {
  257. return ( (getType() == other.getType()) &&
  258. (getData() == other.getData()) );
  259. }
  260. Option::~Option() {
  261. }
  262. } // end of isc::dhcp namespace
  263. } // end of isc namespace