option6_iaaddr.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <stdint.h>
  15. #include <arpa/inet.h>
  16. #include <sstream>
  17. #include "exceptions/exceptions.h"
  18. #include "dhcp/libdhcp++.h"
  19. #include "dhcp/option6_iaaddr.h"
  20. #include "dhcp/dhcp6.h"
  21. #include "asiolink/io_address.h"
  22. #include "util/io_utilities.h"
  23. using namespace std;
  24. using namespace isc;
  25. using namespace isc::dhcp;
  26. using namespace isc::asiolink;
  27. using namespace isc::util;
  28. Option6IAAddr::Option6IAAddr(unsigned short type,
  29. const isc::asiolink::IOAddress& addr,
  30. unsigned int pref, unsigned int valid)
  31. :Option(V6, type), addr_(addr), preferred_(pref),
  32. valid_(valid) {
  33. }
  34. Option6IAAddr::Option6IAAddr(unsigned short type,
  35. boost::shared_array<uint8_t> buf,
  36. unsigned int buf_len, unsigned int offset,
  37. unsigned int option_len)
  38. :Option(V6, type), addr_("::") {
  39. unpack(buf, buf_len, offset, option_len);
  40. }
  41. unsigned int
  42. Option6IAAddr::pack(boost::shared_array<uint8_t>& buf,
  43. unsigned int buf_len,
  44. unsigned int offset) {
  45. if (len() > buf_len) {
  46. isc_throw(OutOfRange, "Failed to pack IA option: len=" << len()
  47. << ", buffer=" << buf_len << ": too small buffer.");
  48. }
  49. uint8_t* ptr = &buf[offset];
  50. ptr = writeUint16(type_, ptr);
  51. // len() returns complete option length. len field contains
  52. // length without 4-byte option header
  53. ptr = writeUint16(len() - OPTION6_HDR_LEN, ptr);
  54. offset += OPTION6_HDR_LEN;
  55. memcpy(ptr, addr_.getAddress().to_v6().to_bytes().data(), 16);
  56. ptr += V6ADDRESS_LEN;
  57. ptr = writeUint32(preferred_, ptr);
  58. ptr = writeUint32(valid_, ptr);
  59. offset += OPTION6_IAADDR_LEN;
  60. // parse suboption (there shouldn't be any)
  61. offset = LibDHCP::packOptions6(buf, buf_len, offset, options_);
  62. return offset;
  63. }
  64. unsigned int
  65. Option6IAAddr::unpack(const boost::shared_array<uint8_t>& buf,
  66. unsigned int buf_len,
  67. unsigned int offset,
  68. unsigned int parse_len) {
  69. if ( parse_len < OPTION6_IAADDR_LEN || offset + OPTION6_IAADDR_LEN > buf_len) {
  70. isc_throw(OutOfRange, "Option " << type_ << " truncated");
  71. }
  72. // 16 bytes: IPv6 address
  73. addr_ = IOAddress::from_bytes(AF_INET6, &buf[offset]);
  74. offset += V6ADDRESS_LEN;
  75. preferred_ = readUint32(&buf[offset]);
  76. offset += sizeof(uint32_t);
  77. valid_ = readUint32(&buf[offset]);
  78. offset += sizeof(uint32_t);
  79. offset = LibDHCP::unpackOptions6(buf, buf_len, offset,
  80. parse_len - 24, options_);
  81. return offset;
  82. }
  83. std::string Option6IAAddr::toText(int indent /* =0 */) {
  84. stringstream tmp;
  85. for (int i=0; i<indent; i++)
  86. tmp << " ";
  87. tmp << "type=" << type_ << "(IAADDR) addr=" << addr_.toText()
  88. << ", preferred-lft=" << preferred_ << ", valid-lft="
  89. << valid_ << endl;
  90. for (OptionCollection::const_iterator opt=options_.begin();
  91. opt!=options_.end();
  92. ++opt) {
  93. tmp << (*opt).second->toText(indent+2);
  94. }
  95. return tmp.str();
  96. }
  97. unsigned short Option6IAAddr::len() {
  98. unsigned short length = OPTION6_HDR_LEN + OPTION6_IAADDR_LEN;
  99. // length of all suboptions
  100. // TODO implement:
  101. // protected: unsigned short Option::lenHelper(int header_size);
  102. for (Option::OptionCollection::iterator it = options_.begin();
  103. it != options_.end();
  104. ++it) {
  105. length += (*it).second->len();
  106. }
  107. return (length);
  108. }