option4_addrlst.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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::dhcp;
  25. using namespace isc::util;
  26. using namespace isc::asiolink;
  27. Option4AddrLst::Option4AddrLst(uint8_t type)
  28. :Option(V4, type) {
  29. }
  30. Option4AddrLst::Option4AddrLst(uint8_t type, const AddressContainer& addrs)
  31. :Option(V4, type) {
  32. setAddresses(addrs);
  33. // don't set addrs_ directly. setAddresses() will do additional checks.
  34. }
  35. Option4AddrLst::Option4AddrLst(uint8_t type,
  36. vector<uint8_t>::const_iterator first,
  37. vector<uint8_t>::const_iterator last)
  38. :Option(V4, type) {
  39. vector<uint8_t> buf = std::vector<uint8_t>(first, last);
  40. if ( (buf.size() % V4ADDRESS_LEN) ) {
  41. isc_throw(OutOfRange, "DHCPv4 Option4AddrLst " << type_
  42. << " has invalid length=" << buf.size()
  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. unsigned short
  97. Option4AddrLst::len() {
  98. // Returns length of the complete option (option header + data length)
  99. return (getHeaderLen() + addrs_.size() * V4ADDRESS_LEN);
  100. }
  101. std::string Option4AddrLst::toText(int indent /* =0 */ ) {
  102. std::stringstream tmp;
  103. for (int i = 0; i < indent; i++)
  104. tmp << " ";
  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. }