option.cc 8.9 KB

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