option.cc 8.8 KB

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