option.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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), data_len_(0) {
  29. }
  30. Option::Option(Universe u, unsigned short type,
  31. const boost::shared_array<uint8_t>& buf,
  32. unsigned int offset, unsigned int len)
  33. :universe_(u), type_(type), data_(buf),
  34. data_len_(len), offset_(offset)
  35. {
  36. // sanity checks
  37. // TODO: universe must be in V4 and V6
  38. }
  39. unsigned int
  40. Option::pack(boost::shared_array<uint8_t>& buf,
  41. unsigned int buf_len,
  42. unsigned int offset) {
  43. switch (universe_) {
  44. case V4:
  45. return pack4(buf, buf_len, offset);
  46. case V6:
  47. return pack6(buf, buf_len, offset);
  48. default:
  49. isc_throw(BadValue, "Unknown universe defined for Option " << type_);
  50. }
  51. }
  52. unsigned int
  53. Option::pack4(boost::shared_array<uint8_t>& buf,
  54. unsigned int buf_len,
  55. unsigned int offset) {
  56. if ( offset+len() > buf_len ) {
  57. isc_throw(OutOfRange, "Failed to pack v4 option=" <<
  58. type_ << ",len=" << data_len_ << ": too small buffer.");
  59. }
  60. uint8_t *ptr = &buf[offset];
  61. ptr[0] = type_;
  62. ptr[1] = data_len_;
  63. ptr += 2;
  64. memcpy(ptr, &data_[0], data_len_);
  65. return offset + len();
  66. }
  67. unsigned int
  68. Option::pack6(boost::shared_array<uint8_t>& buf,
  69. unsigned int buf_len,
  70. unsigned int offset) {
  71. if ( offset+len() > buf_len ) {
  72. isc_throw(OutOfRange, "Failed to pack v6 option=" <<
  73. type_ << ",len=" << len() << ": too small buffer.");
  74. }
  75. uint8_t * ptr = &buf[offset];
  76. ptr = writeUint16(type_, ptr);
  77. ptr = writeUint16(len() - getHeaderLen(), ptr);
  78. if (data_len_)
  79. memcpy(ptr, &data_[offset_], data_len_);
  80. // end of fixed part of this option
  81. offset += OPTION6_HDR_LEN + data_len_;
  82. return LibDHCP::packOptions6(buf, buf_len, offset, options_);
  83. }
  84. unsigned int
  85. Option::unpack(const boost::shared_array<uint8_t>& buf,
  86. unsigned int buf_len,
  87. unsigned int offset,
  88. unsigned int parse_len) {
  89. switch (universe_) {
  90. case V4:
  91. return unpack4(buf, buf_len, offset, parse_len);
  92. case V6:
  93. return unpack6(buf, buf_len, offset, parse_len);
  94. default:
  95. isc_throw(BadValue, "Unknown universe defined for Option " << type_);
  96. }
  97. return 0; // should not happen
  98. }
  99. unsigned int
  100. Option::unpack4(const boost::shared_array<uint8_t>&,
  101. unsigned int ,
  102. unsigned int ,
  103. unsigned int ) {
  104. isc_throw(Unexpected, "IPv4 support not implemented yet.");
  105. return 0;
  106. }
  107. unsigned int
  108. Option::unpack6(const boost::shared_array<uint8_t>& buf,
  109. unsigned int buf_len,
  110. unsigned int offset,
  111. unsigned int parse_len) {
  112. if (buf_len < offset+parse_len) {
  113. isc_throw(OutOfRange, "Failed to unpack DHCPv6 option len="
  114. << parse_len << " offset=" << offset
  115. << " from buffer (length=" << buf_len
  116. << "): too small buffer.");
  117. }
  118. data_ = buf;
  119. offset_ = offset;
  120. data_len_ = buf_len;
  121. return LibDHCP::unpackOptions6(buf, buf_len, offset, parse_len,
  122. options_);
  123. }
  124. unsigned short
  125. Option::len() {
  126. // length of the whole option is header and data stored in this option...
  127. int length = getHeaderLen() + data_len_;
  128. // ... and sum of lengths of all suboptions
  129. for (Option::Option6Collection::iterator it = options_.begin();
  130. it != options_.end();
  131. ++it) {
  132. length += (*it).second->len();
  133. }
  134. // note that this is not equal to lenght field. This value denotes
  135. // number of bytes required to store this option. length option should
  136. // contain (len()-getHeaderLen()) value.
  137. return (length);
  138. }
  139. bool
  140. Option::valid() {
  141. if (universe_ != V4 &&
  142. universe_ != V6) {
  143. return (false);
  144. }
  145. return (true);
  146. }
  147. void
  148. isc::dhcp::Option::addOption(boost::shared_ptr<isc::dhcp::Option> opt) {
  149. options_.insert(pair<int, boost::shared_ptr<Option> >(opt->getType(),
  150. opt));
  151. }
  152. boost::shared_ptr<isc::dhcp::Option>
  153. Option::getOption(unsigned short opt_type) {
  154. isc::dhcp::Option::Option6Collection::const_iterator x =
  155. options_.find(opt_type);
  156. if ( x != options_.end() ) {
  157. return (*x).second;
  158. }
  159. return boost::shared_ptr<isc::dhcp::Option>(); // NULL
  160. }
  161. bool
  162. Option::delOption(unsigned short opt_type) {
  163. isc::dhcp::Option::Option6Collection::iterator x = options_.find(opt_type);
  164. if ( x != options_.end() ) {
  165. options_.erase(x);
  166. return true; // delete successful
  167. }
  168. return (false); // option not found, can't delete
  169. }
  170. std::string Option::toText(int indent /* =0 */ ) {
  171. std::stringstream tmp;
  172. for (int i=0; i<indent; i++)
  173. tmp << " ";
  174. tmp << "type=" << type_ << ", len=" << data_len_ << ": ";
  175. for (unsigned int i=0; i<data_len_; i++) {
  176. if (i) {
  177. tmp << ":";
  178. }
  179. tmp << setfill('0') << setw(2) << hex
  180. << static_cast<unsigned short>(data_[offset_+i]);
  181. }
  182. // print suboptions
  183. for (Option6Collection::const_iterator opt=options_.begin();
  184. opt!=options_.end();
  185. ++opt) {
  186. tmp << (*opt).second->toText(indent+2);
  187. }
  188. return tmp.str();
  189. }
  190. unsigned short
  191. Option::getType() {
  192. return type_;
  193. }
  194. uint8_t*
  195. Option::getData() {
  196. if (data_len_) {
  197. return (&data_[offset_]);
  198. } else {
  199. return (NULL);
  200. }
  201. }
  202. unsigned short
  203. Option::getHeaderLen() {
  204. switch (universe_) {
  205. case V4:
  206. return OPTION4_HDR_LEN; // header length for v4
  207. case V6:
  208. return OPTION6_HDR_LEN; // header length for v6
  209. }
  210. return 0; // should not happen
  211. }
  212. Option::~Option() {
  213. }