option6_ia.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <dhcp/dhcp6.h>
  8. #include <dhcp/libdhcp++.h>
  9. #include <dhcp/option6_ia.h>
  10. #include <dhcp/option_space.h>
  11. #include <exceptions/exceptions.h>
  12. #include <util/io_utilities.h>
  13. #include <arpa/inet.h>
  14. #include <sstream>
  15. #include <stdint.h>
  16. using namespace std;
  17. using namespace isc::util;
  18. namespace isc {
  19. namespace dhcp {
  20. Option6IA::Option6IA(uint16_t type, uint32_t iaid)
  21. :Option(Option::V6, type), iaid_(iaid), t1_(0), t2_(0) {
  22. // IA_TA has different layout than IA_NA and IA_PD. We can't use this class
  23. if (type == D6O_IA_TA) {
  24. isc_throw(BadValue, "Can't use Option6IA for IA_TA as it has "
  25. "a different layout");
  26. }
  27. setEncapsulatedSpace(DHCP6_OPTION_SPACE);
  28. }
  29. Option6IA::Option6IA(uint16_t type, OptionBufferConstIter begin,
  30. OptionBufferConstIter end)
  31. :Option(Option::V6, type) {
  32. // IA_TA has different layout than IA_NA and IA_PD. We can't use this class
  33. if (type == D6O_IA_TA) {
  34. isc_throw(BadValue, "Can't use Option6IA for IA_TA as it has "
  35. "a different layout");
  36. }
  37. setEncapsulatedSpace(DHCP6_OPTION_SPACE);
  38. unpack(begin, end);
  39. }
  40. OptionPtr
  41. Option6IA::clone() const {
  42. return (cloneInternal<Option6IA>());
  43. }
  44. void Option6IA::pack(isc::util::OutputBuffer& buf) const {
  45. buf.writeUint16(type_);
  46. buf.writeUint16(len() - OPTION6_HDR_LEN);
  47. buf.writeUint32(iaid_);
  48. buf.writeUint32(t1_);
  49. buf.writeUint32(t2_);
  50. packOptions(buf);
  51. }
  52. void Option6IA::unpack(OptionBufferConstIter begin,
  53. OptionBufferConstIter end) {
  54. // IA_NA and IA_PD have 12 bytes content (iaid, t1, t2 fields)
  55. // followed by 0 or more sub-options.
  56. if (distance(begin, end) < OPTION6_IA_LEN) {
  57. isc_throw(OutOfRange, "Option " << type_ << " truncated");
  58. }
  59. iaid_ = readUint32(&(*begin), distance(begin, end));
  60. begin += sizeof(uint32_t);
  61. t1_ = readUint32(&(*begin), distance(begin, end));
  62. begin += sizeof(uint32_t);
  63. t2_ = readUint32(&(*begin), distance(begin, end));
  64. begin += sizeof(uint32_t);
  65. unpackOptions(OptionBuffer(begin, end));
  66. }
  67. std::string Option6IA::toText(int indent) const {
  68. stringstream output;
  69. switch(getType()) {
  70. case D6O_IA_NA:
  71. output << headerToText(indent, "IA_NA");
  72. break;
  73. case D6O_IA_PD:
  74. output << headerToText(indent, "IA_PD");
  75. break;
  76. default:
  77. output << headerToText(indent);
  78. }
  79. output << ": iaid=" << iaid_ << ", t1=" << t1_ << ", t2=" << t2_
  80. << suboptionsToText(indent + 2);
  81. return (output.str());
  82. }
  83. uint16_t Option6IA::len() const {
  84. uint16_t length = OPTION6_HDR_LEN /*header (4)*/ +
  85. OPTION6_IA_LEN /* option content (12) */;
  86. // length of all suboptions
  87. for (OptionCollection::const_iterator it = options_.begin();
  88. it != options_.end();
  89. ++it) {
  90. length += (*it).second->len();
  91. }
  92. return (length);
  93. }
  94. } // end of isc::dhcp namespace
  95. } // end of isc namespace