option4_addrlst.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/option4_addrlst.h>
  16. #include <exceptions/exceptions.h>
  17. #include <util/io_utilities.h>
  18. #include <iomanip>
  19. #include <sstream>
  20. #include <arpa/inet.h>
  21. #include <stdint.h>
  22. #include <string.h>
  23. using namespace std;
  24. using namespace isc::util;
  25. using namespace isc::asiolink;
  26. namespace isc {
  27. namespace dhcp {
  28. Option4AddrLst::Option4AddrLst(uint8_t type)
  29. :Option(V4, type) {
  30. }
  31. Option4AddrLst::Option4AddrLst(uint8_t type, const AddressContainer& addrs)
  32. :Option(V4, type) {
  33. setAddresses(addrs);
  34. // don't set addrs_ directly. setAddresses() will do additional checks.
  35. }
  36. Option4AddrLst::Option4AddrLst(uint8_t type, OptionBufferConstIter first,
  37. OptionBufferConstIter last)
  38. :Option(V4, type) {
  39. if ( (distance(first, last) % V4ADDRESS_LEN) ) {
  40. isc_throw(OutOfRange, "DHCPv4 Option4AddrLst " << type_
  41. << " has invalid length=" << distance(first, last)
  42. << ", must be divisible by 4.");
  43. }
  44. while (first != last) {
  45. const uint8_t* ptr = &(*first);
  46. addAddress(IOAddress(readUint32(ptr)));
  47. first += V4ADDRESS_LEN;
  48. }
  49. }
  50. Option4AddrLst::Option4AddrLst(uint8_t type, const IOAddress& addr)
  51. :Option(V4, type) {
  52. setAddress(addr);
  53. }
  54. void
  55. Option4AddrLst::pack4(isc::util::OutputBuffer& buf) {
  56. if (addrs_.size() * V4ADDRESS_LEN > 255) {
  57. isc_throw(OutOfRange, "DHCPv4 Option4AddrLst " << type_ << " is too big."
  58. << "At most 255 bytes are supported.");
  59. /// TODO Larger options can be stored as separate instances
  60. /// of DHCPv4 options. Clients MUST concatenate them.
  61. /// Fortunately, there are no such large options used today.
  62. }
  63. buf.writeUint8(type_);
  64. buf.writeUint8(len() - getHeaderLen());
  65. AddressContainer::const_iterator addr = addrs_.begin();
  66. while (addr != addrs_.end()) {
  67. buf.writeUint32(*addr);
  68. ++addr;
  69. }
  70. }
  71. void Option4AddrLst::setAddress(const isc::asiolink::IOAddress& addr) {
  72. if (!addr.isV4()) {
  73. isc_throw(BadValue, "Can't store non-IPv4 address in "
  74. << "Option4AddrLst option");
  75. }
  76. addrs_.clear();
  77. addAddress(addr);
  78. }
  79. void Option4AddrLst::setAddresses(const AddressContainer& addrs) {
  80. // Do not copy it as a whole. addAddress() does sanity checks.
  81. // i.e. throw if someone tries to set IPv6 address.
  82. addrs_.clear();
  83. for (AddressContainer::const_iterator addr = addrs.begin();
  84. addr != addrs.end(); ++addr) {
  85. addAddress(*addr);
  86. }
  87. }
  88. void Option4AddrLst::addAddress(const isc::asiolink::IOAddress& addr) {
  89. if (!addr.isV4()) {
  90. isc_throw(BadValue, "Can't store non-IPv4 address in "
  91. << "Option4AddrLst option");
  92. }
  93. addrs_.push_back(addr);
  94. }
  95. uint16_t Option4AddrLst::len() {
  96. // Returns length of the complete option (option header + data length)
  97. return (getHeaderLen() + addrs_.size() * V4ADDRESS_LEN);
  98. }
  99. std::string Option4AddrLst::toText(int indent /* =0 */ ) {
  100. std::stringstream tmp;
  101. for (int i = 0; i < indent; i++) {
  102. tmp << " ";
  103. }
  104. tmp << "type=" << type_ << ", len=" << len()-getHeaderLen() << ":";
  105. for (AddressContainer::const_iterator addr = addrs_.begin();
  106. addr != addrs_.end(); ++addr) {
  107. tmp << " " << (*addr);
  108. }
  109. return tmp.str();
  110. }
  111. } // end of isc::dhcp namespace
  112. } // end of isc namespace