option6_iaaddr.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2011-2012 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_iaaddr.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. Option6IAAddr::Option6IAAddr(uint16_t type, const isc::asiolink::IOAddress& addr,
  29. uint32_t pref, uint32_t valid)
  30. :Option(V6, type), addr_(addr), preferred_(pref),
  31. valid_(valid) {
  32. }
  33. Option6IAAddr::Option6IAAddr(uint32_t type, OptionBuffer::const_iterator begin,
  34. OptionBuffer::const_iterator end)
  35. :Option(V6, type), addr_("::") {
  36. unpack(begin, end);
  37. }
  38. void Option6IAAddr::pack(isc::util::OutputBuffer& buf) {
  39. buf.writeUint16(type_);
  40. // len() returns complete option length. len field contains
  41. // length without 4-byte option header
  42. buf.writeUint16(len() - getHeaderLen());
  43. buf.writeData(addr_.getAddress().to_v6().to_bytes().data(),
  44. isc::asiolink::V6ADDRESS_LEN);
  45. buf.writeUint32(preferred_);
  46. buf.writeUint32(valid_);
  47. // parse suboption (there shouldn't be any for IAADDR)
  48. LibDHCP::packOptions6(buf, options_);
  49. }
  50. void Option6IAAddr::unpack(OptionBuffer::const_iterator begin,
  51. OptionBuffer::const_iterator end) {
  52. if ( distance(begin, end) < OPTION6_IAADDR_LEN) {
  53. isc_throw(OutOfRange, "Option " << type_ << " truncated");
  54. }
  55. // 16 bytes: IPv6 address
  56. addr_ = IOAddress::fromBytes(AF_INET6, &(*begin));
  57. begin += V6ADDRESS_LEN;
  58. preferred_ = readUint32( &(*begin) );
  59. begin += sizeof(uint32_t);
  60. valid_ = readUint32( &(*begin) );
  61. begin += sizeof(uint32_t);
  62. LibDHCP::unpackOptions6(OptionBuffer(begin, end), options_);
  63. }
  64. std::string Option6IAAddr::toText(int indent /* =0 */) {
  65. stringstream tmp;
  66. for (int i=0; i<indent; i++)
  67. tmp << " ";
  68. tmp << "type=" << type_ << "(IAADDR) addr=" << addr_.toText()
  69. << ", preferred-lft=" << preferred_ << ", valid-lft="
  70. << valid_ << endl;
  71. for (OptionCollection::const_iterator opt=options_.begin();
  72. opt!=options_.end();
  73. ++opt) {
  74. tmp << (*opt).second->toText(indent+2);
  75. }
  76. return tmp.str();
  77. }
  78. uint16_t Option6IAAddr::len() {
  79. uint16_t length = OPTION6_HDR_LEN + OPTION6_IAADDR_LEN;
  80. // length of all suboptions
  81. // TODO implement:
  82. // protected: unsigned short Option::lenHelper(int header_size);
  83. for (Option::OptionCollection::iterator it = options_.begin();
  84. it != options_.end();
  85. ++it) {
  86. length += (*it).second->len();
  87. }
  88. return (length);
  89. }
  90. } // end of namespace isc::dhcp
  91. } // end of namespace isc