option.cc 7.8 KB

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