option6_iaprefix.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (C) 2013 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 <asiolink/io_address.h>
  15. #include <dhcp/dhcp6.h>
  16. #include <dhcp/libdhcp++.h>
  17. #include <dhcp/option6_iaprefix.h>
  18. #include <exceptions/exceptions.h>
  19. #include <util/io_utilities.h>
  20. #include <sstream>
  21. #include <stdint.h>
  22. #include <arpa/inet.h>
  23. using namespace std;
  24. using namespace isc::asiolink;
  25. using namespace isc::util;
  26. namespace isc {
  27. namespace dhcp {
  28. Option6IAPrefix::Option6IAPrefix(uint16_t type, const isc::asiolink::IOAddress& prefix,
  29. uint8_t prefix_len, uint32_t pref, uint32_t valid)
  30. :Option6IAAddr(type, prefix, pref, valid), prefix_len_(prefix_len) {
  31. setEncapsulatedSpace("dhcp6");
  32. // Option6IAAddr will check if prefix is IPv6 and will throw if it is not
  33. if (prefix_len > 128) {
  34. isc_throw(BadValue, prefix_len << " is not a valid prefix length. "
  35. << "Allowed range is 0..128");
  36. }
  37. }
  38. Option6IAPrefix::Option6IAPrefix(uint32_t type, OptionBuffer::const_iterator begin,
  39. OptionBuffer::const_iterator end)
  40. :Option6IAAddr(type, begin, end) {
  41. setEncapsulatedSpace("dhcp6");
  42. unpack(begin, end);
  43. }
  44. void Option6IAPrefix::pack(isc::util::OutputBuffer& buf) {
  45. if (!addr_.isV6()) {
  46. isc_throw(isc::BadValue, addr_ << " is not an IPv6 address");
  47. }
  48. buf.writeUint16(type_);
  49. // len() returns complete option length. len field contains
  50. // length without 4-byte option header
  51. buf.writeUint16(len() - getHeaderLen());
  52. buf.writeUint32(preferred_);
  53. buf.writeUint32(valid_);
  54. buf.writeUint8(prefix_len_);
  55. buf.writeData(&addr_.toBytes()[0], isc::asiolink::V6ADDRESS_LEN);
  56. // store encapsulated options (the only defined so far is PD_EXCLUDE)
  57. packOptions(buf);
  58. }
  59. void Option6IAPrefix::unpack(OptionBuffer::const_iterator begin,
  60. OptionBuffer::const_iterator end) {
  61. if ( distance(begin, end) < OPTION6_IAPREFIX_LEN) {
  62. isc_throw(OutOfRange, "Option " << type_ << " truncated");
  63. }
  64. preferred_ = readUint32( &(*begin) );
  65. begin += sizeof(uint32_t);
  66. valid_ = readUint32( &(*begin) );
  67. begin += sizeof(uint32_t);
  68. prefix_len_ = *begin;
  69. begin += sizeof(uint8_t);
  70. // 16 bytes: IPv6 address
  71. addr_ = IOAddress::fromBytes(AF_INET6, &(*begin));
  72. begin += V6ADDRESS_LEN;
  73. // unpack encapsulated options (the only defined so far is PD_EXCLUDE)
  74. unpackOptions(OptionBuffer(begin, end));
  75. }
  76. std::string Option6IAPrefix::toText(int indent /* =0 */) {
  77. stringstream tmp;
  78. for (int i=0; i<indent; i++)
  79. tmp << " ";
  80. tmp << "type=" << type_ << "(IAPREFIX) prefix=" << addr_ << "/"
  81. << prefix_len_ << ", preferred-lft=" << preferred_ << ", valid-lft="
  82. << valid_ << endl;
  83. for (OptionCollection::const_iterator opt=options_.begin();
  84. opt!=options_.end();
  85. ++opt) {
  86. tmp << (*opt).second->toText(indent + 2);
  87. }
  88. return tmp.str();
  89. }
  90. uint16_t Option6IAPrefix::len() {
  91. uint16_t length = OPTION6_HDR_LEN + OPTION6_IAPREFIX_LEN;
  92. // length of all suboptions
  93. for (OptionCollection::const_iterator it = options_.begin();
  94. it != options_.end(); ++it) {
  95. length += (*it).second->len();
  96. }
  97. return (length);
  98. }
  99. } // end of namespace isc::dhcp
  100. } // end of namespace isc