option6_ia.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <exceptions/exceptions.h>
  11. #include <util/io_utilities.h>
  12. #include <arpa/inet.h>
  13. #include <sstream>
  14. #include <stdint.h>
  15. using namespace std;
  16. using namespace isc::util;
  17. namespace isc {
  18. namespace dhcp {
  19. Option6IA::Option6IA(uint16_t type, uint32_t iaid)
  20. :Option(Option::V6, type), iaid_(iaid), t1_(0), t2_(0) {
  21. // IA_TA has different layout than IA_NA and IA_PD. We can't sue this class
  22. if (type == D6O_IA_TA) {
  23. isc_throw(BadValue, "Can't use Option6IA for IA_TA as it has "
  24. "a different layout");
  25. }
  26. setEncapsulatedSpace("dhcp6");
  27. }
  28. Option6IA::Option6IA(uint16_t type, OptionBufferConstIter begin,
  29. OptionBufferConstIter end)
  30. :Option(Option::V6, type) {
  31. // IA_TA has different layout than IA_NA and IA_PD. We can't use this class
  32. if (type == D6O_IA_TA) {
  33. isc_throw(BadValue, "Can't use Option6IA for IA_TA as it has "
  34. "a different layout");
  35. }
  36. setEncapsulatedSpace("dhcp6");
  37. unpack(begin, end);
  38. }
  39. OptionPtr
  40. Option6IA::clone() const {
  41. return (cloneInternal<Option6IA>());
  42. }
  43. void Option6IA::pack(isc::util::OutputBuffer& buf) const {
  44. buf.writeUint16(type_);
  45. buf.writeUint16(len() - OPTION6_HDR_LEN);
  46. buf.writeUint32(iaid_);
  47. buf.writeUint32(t1_);
  48. buf.writeUint32(t2_);
  49. packOptions(buf);
  50. }
  51. void Option6IA::unpack(OptionBufferConstIter begin,
  52. OptionBufferConstIter end) {
  53. // IA_NA and IA_PD have 12 bytes content (iaid, t1, t2 fields)
  54. // followed by 0 or more sub-options.
  55. if (distance(begin, end) < OPTION6_IA_LEN) {
  56. isc_throw(OutOfRange, "Option " << type_ << " truncated");
  57. }
  58. iaid_ = readUint32(&(*begin), distance(begin, end));
  59. begin += sizeof(uint32_t);
  60. t1_ = readUint32(&(*begin), distance(begin, end));
  61. begin += sizeof(uint32_t);
  62. t2_ = readUint32(&(*begin), distance(begin, end));
  63. begin += sizeof(uint32_t);
  64. unpackOptions(OptionBuffer(begin, end));
  65. }
  66. std::string Option6IA::toText(int indent) const {
  67. stringstream output;
  68. switch(getType()) {
  69. case D6O_IA_NA:
  70. output << headerToText(indent, "IA_NA");
  71. break;
  72. case D6O_IA_PD:
  73. output << headerToText(indent, "IA_PD");
  74. break;
  75. default:
  76. output << headerToText(indent);
  77. }
  78. output << ": iaid=" << iaid_ << ", t1=" << t1_ << ", t2=" << t2_
  79. << suboptionsToText(indent + 2);
  80. return (output.str());
  81. }
  82. uint16_t Option6IA::len() const {
  83. uint16_t length = OPTION6_HDR_LEN /*header (4)*/ +
  84. OPTION6_IA_LEN /* option content (12) */;
  85. // length of all suboptions
  86. for (OptionCollection::const_iterator it = options_.begin();
  87. it != options_.end();
  88. ++it) {
  89. length += (*it).second->len();
  90. }
  91. return (length);
  92. }
  93. } // end of isc::dhcp namespace
  94. } // end of isc namespace