option4_addrlst.cc 4.2 KB

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