option6_iaaddr.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. if (!addr_.isV6()) {
  44. isc_throw(isc::BadValue, addr_.toText()
  45. << " is not an IPv6 address");
  46. }
  47. buf.writeData(&addr_.toBytes()[0], isc::asiolink::V6ADDRESS_LEN);
  48. buf.writeUint32(preferred_);
  49. buf.writeUint32(valid_);
  50. // parse suboption (there shouldn't be any for IAADDR)
  51. packOptions(buf);
  52. }
  53. void Option6IAAddr::unpack(OptionBuffer::const_iterator begin,
  54. OptionBuffer::const_iterator end) {
  55. if ( distance(begin, end) < OPTION6_IAADDR_LEN) {
  56. isc_throw(OutOfRange, "Option " << type_ << " truncated");
  57. }
  58. // 16 bytes: IPv6 address
  59. addr_ = IOAddress::fromBytes(AF_INET6, &(*begin));
  60. begin += V6ADDRESS_LEN;
  61. preferred_ = readUint32( &(*begin) );
  62. begin += sizeof(uint32_t);
  63. valid_ = readUint32( &(*begin) );
  64. begin += sizeof(uint32_t);
  65. unpackOptions(OptionBuffer(begin, end));
  66. }
  67. std::string Option6IAAddr::toText(int indent /* =0 */) {
  68. stringstream tmp;
  69. for (int i=0; i<indent; i++)
  70. tmp << " ";
  71. tmp << "type=" << type_ << "(IAADDR) addr=" << addr_.toText()
  72. << ", preferred-lft=" << preferred_ << ", valid-lft="
  73. << valid_ << endl;
  74. for (OptionCollection::const_iterator opt=options_.begin();
  75. opt!=options_.end();
  76. ++opt) {
  77. tmp << (*opt).second->toText(indent+2);
  78. }
  79. return tmp.str();
  80. }
  81. uint16_t Option6IAAddr::len() {
  82. uint16_t length = OPTION6_HDR_LEN + OPTION6_IAADDR_LEN;
  83. // length of all suboptions
  84. // TODO implement:
  85. // protected: unsigned short Option::lenHelper(int header_size);
  86. for (Option::OptionCollection::iterator it = options_.begin();
  87. it != options_.end();
  88. ++it) {
  89. length += (*it).second->len();
  90. }
  91. return (length);
  92. }
  93. } // end of namespace isc::dhcp
  94. } // end of namespace isc