option.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 <string.h>
  15. #include <stdint.h>
  16. #include <arpa/inet.h>
  17. #include <sstream>
  18. #include <iomanip>
  19. #include "exceptions/exceptions.h"
  20. #include "util/io_utilities.h"
  21. #include "dhcp/option.h"
  22. #include "dhcp/libdhcp++.h"
  23. using namespace std;
  24. using namespace isc::util;
  25. namespace isc {
  26. namespace dhcp {
  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_(OptionBuffer(first,last)) {
  42. check();
  43. }
  44. void
  45. Option::check() {
  46. if ( (universe_ != V4) && (universe_ != V6) ) {
  47. isc_throw(BadValue, "Invalid universe type specified."
  48. << "Only V4 and V6 are allowed.");
  49. }
  50. if (universe_ == V4) {
  51. if (type_ > 255) {
  52. isc_throw(OutOfRange, "DHCPv4 Option type " << type_ << " is too big."
  53. << "For DHCPv4 allowed type range is 0..255");
  54. } else if (data_.size() > 255) {
  55. isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big.");
  56. /// TODO Larger options can be stored as separate instances
  57. /// of DHCPv4 options. Clients MUST concatenate them.
  58. /// Fortunately, there are no such large options used today.
  59. }
  60. }
  61. // no need to check anything for DHCPv6. It allows full range (0-64k) of
  62. // both types and data size.
  63. }
  64. void Option::pack(isc::util::OutputBuffer& buf) {
  65. switch (universe_) {
  66. case V6:
  67. return (pack6(buf));
  68. case V4:
  69. return (pack4(buf));
  70. default:
  71. isc_throw(BadValue, "Failed to pack " << type_ << " option. Do not "
  72. << "use this method for options other than DHCPv6.");
  73. }
  74. }
  75. void
  76. Option::pack4(isc::util::OutputBuffer& buf) {
  77. switch (universe_) {
  78. case V4: {
  79. if (len() > 255) {
  80. isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big."
  81. << "At most 255 bytes are supported.");
  82. /// TODO Larger options can be stored as separate instances
  83. /// of DHCPv4 options. Clients MUST concatenate them.
  84. /// Fortunately, there are no such large options used today.
  85. }
  86. buf.writeUint8(type_);
  87. buf.writeUint8(len() - getHeaderLen());
  88. buf.writeData(&data_[0], data_.size());
  89. LibDHCP::packOptions(buf, options_);
  90. return;
  91. }
  92. case V6:
  93. /// TODO: Do we need a sanity check for option size here?
  94. buf.writeUint16(type_);
  95. buf.writeUint16(len() - getHeaderLen());
  96. LibDHCP::packOptions(buf, options_);
  97. return;
  98. default:
  99. isc_throw(OutOfRange, "Invalid universe type" << universe_);
  100. }
  101. }
  102. void Option::pack6(isc::util::OutputBuffer& buf) {
  103. buf.writeUint16(type_);
  104. buf.writeUint16(len() - getHeaderLen());
  105. if (! data_.empty()) {
  106. buf.writeData(&data_[0], data_.size());
  107. }
  108. return LibDHCP::packOptions6(buf, options_);
  109. }
  110. void Option::unpack(OptionBufferConstIter begin,
  111. OptionBufferConstIter end) {
  112. data_ = OptionBuffer(begin, end);
  113. }
  114. uint16_t Option::len() {
  115. // Returns length of the complete option (data length + DHCPv4/DHCPv6
  116. // option header)
  117. // length of the whole option is header and data stored in this option...
  118. int length = getHeaderLen() + data_.size();
  119. // ... and sum of lengths of all suboptions
  120. for (Option::OptionCollection::iterator it = options_.begin();
  121. it != options_.end();
  122. ++it) {
  123. length += (*it).second->len();
  124. }
  125. // note that this is not equal to lenght field. This value denotes
  126. // number of bytes required to store this option. length option should
  127. // contain (len()-getHeaderLen()) value.
  128. return (length);
  129. }
  130. bool
  131. Option::valid() {
  132. if (universe_ != V4 &&
  133. universe_ != V6) {
  134. return (false);
  135. }
  136. return (true);
  137. }
  138. OptionPtr Option::getOption(uint16_t opt_type) {
  139. isc::dhcp::Option::OptionCollection::const_iterator x =
  140. options_.find(opt_type);
  141. if ( x != options_.end() ) {
  142. return (*x).second;
  143. }
  144. return OptionPtr(); // NULL
  145. }
  146. bool Option::delOption(uint16_t opt_type) {
  147. isc::dhcp::Option::OptionCollection::iterator x = options_.find(opt_type);
  148. if ( x != options_.end() ) {
  149. options_.erase(x);
  150. return true; // delete successful
  151. }
  152. return (false); // option not found, can't delete
  153. }
  154. std::string Option::toText(int indent /* =0 */ ) {
  155. std::stringstream tmp;
  156. for (int i = 0; i < indent; i++)
  157. tmp << " ";
  158. tmp << "type=" << type_ << ", len=" << len()-getHeaderLen() << ": ";
  159. for (unsigned int i = 0; i < data_.size(); i++) {
  160. if (i) {
  161. tmp << ":";
  162. }
  163. tmp << setfill('0') << setw(2) << hex
  164. << static_cast<unsigned short>(data_[i]);
  165. }
  166. // print suboptions
  167. for (OptionCollection::const_iterator opt = options_.begin();
  168. opt != options_.end();
  169. ++opt) {
  170. tmp << (*opt).second->toText(indent+2);
  171. }
  172. return tmp.str();
  173. }
  174. uint16_t
  175. Option::getHeaderLen() {
  176. switch (universe_) {
  177. case V4:
  178. return OPTION4_HDR_LEN; // header length for v4
  179. case V6:
  180. return OPTION6_HDR_LEN; // header length for v6
  181. }
  182. return 0; // should not happen
  183. }
  184. void Option::addOption(OptionPtr opt) {
  185. if (universe_ == V4) {
  186. // check for uniqueness (DHCPv4 options must be unique)
  187. if (getOption(opt->getType())) {
  188. isc_throw(BadValue, "Option " << opt->getType()
  189. << " already present in this message.");
  190. }
  191. }
  192. options_.insert(make_pair(opt->getType(), opt));
  193. }
  194. uint8_t Option::getUint8() {
  195. if (data_.size() < sizeof(uint8_t) ) {
  196. isc_throw(OutOfRange, "Attempt to read uint8 from option " << type_
  197. << " that has size " << data_.size());
  198. }
  199. return (data_[0]);
  200. }
  201. uint16_t Option::getUint16() {
  202. if (data_.size() < sizeof(uint16_t) ) {
  203. isc_throw(OutOfRange, "Attempt to read uint16 from option " << type_
  204. << " that has size " << data_.size());
  205. }
  206. return ( readUint16(&data_[0]) );
  207. }
  208. uint32_t Option::getUint32() {
  209. if (data_.size() < sizeof(uint32_t) ) {
  210. isc_throw(OutOfRange, "Attempt to read uint32 from option " << type_
  211. << " that has size " << data_.size());
  212. }
  213. return ( readUint32(&data_[0]) );
  214. }
  215. void Option::setUint8(uint8_t value) {
  216. data_.resize(1);
  217. data_[0] = value;
  218. }
  219. void Option::setUint16(uint16_t value) {
  220. data_.resize(2);
  221. writeUint16(value, &data_[0]);
  222. }
  223. void Option::setUint32(uint32_t value) {
  224. data_.resize(4);
  225. writeUint32(value, &data_[0]);
  226. }
  227. void Option::setData(const OptionBufferConstIter first,
  228. const OptionBufferConstIter last) {
  229. // We will copy entire option buffer, so we have to resize data_.
  230. data_.resize(std::distance(first, last));
  231. std::copy(first, last, data_.begin());
  232. }
  233. Option::~Option() {
  234. }
  235. } // end of isc::dhcp namespace
  236. } // end of isc namespace