option.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Copyright (C) 2011 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 <boost/shared_array.hpp>
  20. #include "exceptions/exceptions.h"
  21. #include "util/io_utilities.h"
  22. #include "dhcp/option.h"
  23. #include "dhcp/libdhcp.h"
  24. using namespace std;
  25. using namespace isc::dhcp;
  26. using namespace isc::util;
  27. Option::Option(Universe u, unsigned short 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, unsigned short type,
  35. const boost::shared_array<uint8_t>& buf,
  36. unsigned int offset, unsigned int len)
  37. :universe_(u), type_(type),
  38. offset_(offset)
  39. {
  40. if ( (u == V4) && (type > 255)) {
  41. isc_throw(BadValue, "Can't create V4 option of type "
  42. << type << ", V4 options are in range 0..255");
  43. }
  44. uint8_t* ptr = &buf[offset];
  45. data_ = std::vector<uint8_t>(ptr, ptr + len);
  46. // sanity checks
  47. // TODO: universe must be in V4 and V6
  48. }
  49. Option::Option(Universe u, unsigned short type, std::vector<uint8_t>& data)
  50. :universe_(u), type_(type), data_(data) {
  51. if ( (u == V4) && (data.size() > 255) ) {
  52. isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big."
  53. << "At most 255 bytes are supported.");
  54. /// TODO Larger options can be stored as separate instances
  55. /// of DHCPv4 options. Clients MUST concatenate them.
  56. /// Fortunately, there are no such large options used today.
  57. }
  58. if ( (u == V4) && (type > 255) ) {
  59. isc_throw(OutOfRange, "DHCPv4 Option type " << type_ << " is too big."
  60. << "For DHCPv4 allowed type range is 0..255");
  61. }
  62. }
  63. Option::Option(Universe u, uint16_t type, vector<uint8_t>::const_iterator first,
  64. vector<uint8_t>::const_iterator last)
  65. :universe_(u), type_(type) {
  66. if ( (u == V4) && (type > 255) ) {
  67. isc_throw(OutOfRange, "DHCPv4 Option type " << type_ << " is too big."
  68. << "For DHCPv4 allowed type range is 0..255");
  69. }
  70. data_ = std::vector<uint8_t>(first, last);
  71. if ( (u == V4) && (data_.size() > 255) ) {
  72. isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big.");
  73. }
  74. }
  75. unsigned int
  76. Option::pack(boost::shared_array<uint8_t>& buf,
  77. unsigned int buf_len,
  78. unsigned int offset) {
  79. if (universe_ != V6) {
  80. isc_throw(BadValue, "Failed to pack " << type_ << " option. Do not "
  81. << "use this method for options other than DHCPv6.");
  82. }
  83. return pack6(buf, buf_len, offset);
  84. }
  85. void
  86. Option::pack4(isc::util::OutputBuffer& buf) {
  87. switch (universe_) {
  88. case V4: {
  89. if (data_.size() > 255) {
  90. isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big."
  91. << "At most 255 bytes are supported.");
  92. /// TODO Larger options can be stored as separate instances
  93. /// of DHCPv4 options. Clients MUST concatenate them.
  94. /// Fortunately, there are no such large options used today.
  95. }
  96. buf.writeUint8(type_);
  97. buf.writeUint8(len() - getHeaderLen());
  98. buf.writeData(&data_[0], data_.size());
  99. LibDHCP::packOptions(buf, options_);
  100. return;
  101. }
  102. case V6:
  103. /// TODO: Do we need a sanity check for option size here?
  104. buf.writeUint16(type_);
  105. buf.writeUint16(len() - getHeaderLen());
  106. LibDHCP::packOptions(buf, options_);
  107. return;
  108. default:
  109. isc_throw(OutOfRange, "Invalid universe type" << universe_);
  110. }
  111. }
  112. unsigned int
  113. Option::pack4(boost::shared_array<uint8_t>& buf,
  114. unsigned int buf_len,
  115. unsigned int offset) {
  116. if (offset + len() > buf_len) {
  117. isc_throw(OutOfRange, "Failed to pack v4 option=" <<
  118. type_ << ",len=" << len() << ": too small buffer.");
  119. }
  120. uint8_t *ptr = &buf[offset];
  121. ptr[0] = type_;
  122. ptr[1] = len() - getHeaderLen();
  123. ptr += 2;
  124. memcpy(ptr, &data_[0], data_.size());
  125. return offset + len();
  126. }
  127. unsigned int
  128. Option::pack6(boost::shared_array<uint8_t>& buf,
  129. unsigned int buf_len,
  130. unsigned int offset) {
  131. if (offset+len() > buf_len) {
  132. isc_throw(OutOfRange, "Failed to pack v6 option=" <<
  133. type_ << ",len=" << len() << ": too small buffer.");
  134. }
  135. uint8_t * ptr = &buf[offset];
  136. ptr = writeUint16(type_, ptr);
  137. ptr = writeUint16(len() - getHeaderLen(), ptr);
  138. if (! data_.empty())
  139. memcpy(ptr, &data_[0], data_.size());
  140. // end of fixed part of this option
  141. offset += OPTION6_HDR_LEN + data_.size();
  142. return LibDHCP::packOptions6(buf, buf_len, offset, options_);
  143. }
  144. unsigned int
  145. Option::unpack(const boost::shared_array<uint8_t>& buf,
  146. unsigned int buf_len,
  147. unsigned int offset,
  148. unsigned int parse_len) {
  149. switch (universe_) {
  150. case V4:
  151. return unpack4(buf, buf_len, offset, parse_len);
  152. case V6:
  153. return unpack6(buf, buf_len, offset, parse_len);
  154. default:
  155. isc_throw(BadValue, "Unknown universe defined for Option " << type_);
  156. }
  157. return 0; // should not happen
  158. }
  159. unsigned int
  160. Option::unpack4(const boost::shared_array<uint8_t>&,
  161. unsigned int ,
  162. unsigned int ,
  163. unsigned int ) {
  164. isc_throw(Unexpected, "IPv4 support not implemented yet.");
  165. return 0;
  166. }
  167. unsigned int
  168. Option::unpack6(const boost::shared_array<uint8_t>& buf,
  169. unsigned int buf_len,
  170. unsigned int offset,
  171. unsigned int parse_len) {
  172. if (buf_len < offset+parse_len) {
  173. isc_throw(OutOfRange, "Failed to unpack DHCPv6 option len="
  174. << parse_len << " offset=" << offset
  175. << " from buffer (length=" << buf_len
  176. << "): too small buffer.");
  177. }
  178. uint8_t* ptr = &buf[offset];
  179. data_ = std::vector<uint8_t>(ptr, ptr + parse_len);
  180. offset_ = offset;
  181. return (offset+parse_len);
  182. //return LibDHCP::unpackOptions6(buf, buf_len, offset, parse_len,
  183. // options_);
  184. }
  185. /// Returns length of the complete option (data length + DHCPv4/DHCPv6
  186. /// option header)
  187. unsigned short
  188. Option::len() {
  189. // length of the whole option is header and data stored in this option...
  190. int length = getHeaderLen() + data_.size();
  191. // ... and sum of lengths of all suboptions
  192. for (Option::OptionCollection::iterator it = options_.begin();
  193. it != options_.end();
  194. ++it) {
  195. length += (*it).second->len();
  196. }
  197. // note that this is not equal to lenght field. This value denotes
  198. // number of bytes required to store this option. length option should
  199. // contain (len()-getHeaderLen()) value.
  200. return (length);
  201. }
  202. bool
  203. Option::valid() {
  204. if (universe_ != V4 &&
  205. universe_ != V6) {
  206. return (false);
  207. }
  208. return (true);
  209. }
  210. boost::shared_ptr<isc::dhcp::Option>
  211. Option::getOption(unsigned short opt_type) {
  212. isc::dhcp::Option::OptionCollection::const_iterator x =
  213. options_.find(opt_type);
  214. if ( x != options_.end() ) {
  215. return (*x).second;
  216. }
  217. return boost::shared_ptr<isc::dhcp::Option>(); // NULL
  218. }
  219. bool
  220. Option::delOption(unsigned short opt_type) {
  221. isc::dhcp::Option::OptionCollection::iterator x = options_.find(opt_type);
  222. if ( x != options_.end() ) {
  223. options_.erase(x);
  224. return true; // delete successful
  225. }
  226. return (false); // option not found, can't delete
  227. }
  228. std::string Option::toText(int indent /* =0 */ ) {
  229. std::stringstream tmp;
  230. for (int i = 0; i < indent; i++)
  231. tmp << " ";
  232. tmp << "type=" << type_ << ", len=" << len()-getHeaderLen() << ": ";
  233. for (unsigned int i = 0; i < data_.size(); i++) {
  234. if (i) {
  235. tmp << ":";
  236. }
  237. tmp << setfill('0') << setw(2) << hex
  238. << static_cast<unsigned short>(data_[i]);
  239. }
  240. // print suboptions
  241. for (OptionCollection::const_iterator opt = options_.begin();
  242. opt != options_.end();
  243. ++opt) {
  244. tmp << (*opt).second->toText(indent+2);
  245. }
  246. return tmp.str();
  247. }
  248. unsigned short
  249. Option::getType() {
  250. return type_;
  251. }
  252. const std::vector<uint8_t>&
  253. Option::getData() {
  254. return (data_);
  255. }
  256. unsigned short
  257. Option::getHeaderLen() {
  258. switch (universe_) {
  259. case V4:
  260. return OPTION4_HDR_LEN; // header length for v4
  261. case V6:
  262. return OPTION6_HDR_LEN; // header length for v6
  263. }
  264. return 0; // should not happen
  265. }
  266. void
  267. Option::addOption(boost::shared_ptr<Option> opt) {
  268. if (universe_ == V4) {
  269. // check for uniqueness (DHCPv4 options must be unique)
  270. if (getOption(opt->getType())) {
  271. isc_throw(BadValue, "Option " << opt->getType()
  272. << " already present in this message.");
  273. }
  274. }
  275. options_.insert(pair<int, boost::shared_ptr<Option> >(opt->getType(), opt));
  276. }
  277. Option::~Option() {
  278. }