option6_addrlst.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "asiolink/io_address.h"
  19. #include "util/io_utilities.h"
  20. #include "dhcp/libdhcp.h"
  21. #include "dhcp/option6_addrlst.h"
  22. #include "dhcp/dhcp6.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. Option6AddrLst::Option6AddrLst(unsigned short type,
  29. const AddressContainer& addrs)
  30. :Option(V6, type), addrs_(addrs) {
  31. }
  32. Option6AddrLst::Option6AddrLst(unsigned short type,
  33. const isc::asiolink::IOAddress& addr)
  34. :Option(V6, type), addrs_(1,addr) {
  35. }
  36. Option6AddrLst::Option6AddrLst(unsigned short type,
  37. boost::shared_array<uint8_t> buf,
  38. unsigned int buf_len,
  39. unsigned int offset,
  40. unsigned int option_len)
  41. :Option(V6, type) {
  42. unpack(buf, buf_len, offset, option_len);
  43. }
  44. void
  45. Option6AddrLst::setAddress(const isc::asiolink::IOAddress& addr) {
  46. if (addr.getFamily() != AF_INET6) {
  47. isc_throw(BadValue, "Can't store non-IPv6 address in Option6AddrLst option");
  48. }
  49. addrs_.clear();
  50. addrs_.push_back(addr);
  51. }
  52. void
  53. Option6AddrLst::setAddresses(const AddressContainer& addrs) {
  54. addrs_ = addrs;
  55. }
  56. unsigned int
  57. Option6AddrLst::pack(boost::shared_array<uint8_t>& buf,
  58. unsigned int buf_len,
  59. unsigned int offset) {
  60. if (len() > buf_len) {
  61. isc_throw(OutOfRange, "Failed to pack IA option: len=" << len()
  62. << ", buffer=" << buf_len << ": too small buffer.");
  63. }
  64. writeUint16(type_, &buf[offset]);
  65. offset += sizeof(uint16_t);
  66. // len() returns complete option length.
  67. // len field contains length without 4-byte option header
  68. writeUint16(len() - OPTION6_HDR_LEN, &buf[offset]);
  69. offset += sizeof(uint16_t);
  70. // this wrapping is *ugly*. I wish there was a a
  71. for (AddressContainer::const_iterator addr=addrs_.begin();
  72. addr!=addrs_.end();
  73. ++addr) {
  74. memcpy(&buf[offset],
  75. addr->getAddress().to_v6().to_bytes().data(),
  76. V6ADDRESS_LEN);
  77. offset += V6ADDRESS_LEN;
  78. }
  79. return offset;
  80. }
  81. unsigned int
  82. Option6AddrLst::unpack(const boost::shared_array<uint8_t>& buf,
  83. unsigned int buf_len,
  84. unsigned int offset,
  85. unsigned int option_len) {
  86. if (offset+option_len > buf_len) {
  87. isc_throw(OutOfRange, "Option " << type_
  88. << " truncated.");
  89. }
  90. if (option_len%16) {
  91. isc_throw(OutOfRange, "Option " << type_
  92. << " malformed: len=" << option_len
  93. << " is not divisible by 16.");
  94. }
  95. while (option_len > 0) {
  96. addrs_.push_back(IOAddress::from_bytes(AF_INET6, &buf[offset]));
  97. offset += 16;
  98. option_len -= 16;
  99. }
  100. return offset;
  101. }
  102. std::string Option6AddrLst::toText(int indent /* =0 */) {
  103. stringstream tmp;
  104. for (int i=0; i<indent; i++)
  105. tmp << " ";
  106. tmp << "type=" << type_ << " " << addrs_.size() << "addr(s): ";
  107. for (AddressContainer::const_iterator addr=addrs_.begin();
  108. addr!=addrs_.end();
  109. ++addr) {
  110. tmp << addr->toText() << " ";
  111. }
  112. return tmp.str();
  113. }
  114. uint16_t Option6AddrLst::len() {
  115. return (OPTION6_HDR_LEN + addrs_.size()*16);
  116. }