Commit ee88ec85 authored by Marcin Siodelski's avatar Marcin Siodelski
Browse files

[4497] Fix constness of methods in Option classes.

parent 164e41a5
......@@ -126,7 +126,7 @@ public:
/// \brief Checks if option is valid.
///
/// \return true, if option is valid.
virtual bool valid() {
virtual bool valid() const {
return (Option::valid() && option_valid_);
}
......
......@@ -54,7 +54,7 @@ Option::Option(Universe u, uint16_t type, OptionBufferConstIter first,
}
void
Option::check() {
Option::check() const {
if ( (universe_ != V4) && (universe_ != V6) ) {
isc_throw(BadValue, "Invalid universe type specified. "
<< "Only V4 and V6 are allowed.");
......@@ -77,7 +77,7 @@ Option::check() {
// both types and data size.
}
void Option::pack(isc::util::OutputBuffer& buf) {
void Option::pack(isc::util::OutputBuffer& buf) const {
// Write a header.
packHeader(buf);
// Write data.
......@@ -89,7 +89,7 @@ void Option::pack(isc::util::OutputBuffer& buf) {
}
void
Option::packHeader(isc::util::OutputBuffer& buf) {
Option::packHeader(isc::util::OutputBuffer& buf) const {
if (universe_ == V4) {
if (len() > 255) {
isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big. "
......@@ -109,7 +109,7 @@ Option::packHeader(isc::util::OutputBuffer& buf) {
}
void
Option::packOptions(isc::util::OutputBuffer& buf) {
Option::packOptions(isc::util::OutputBuffer& buf) const {
switch (universe_) {
case V4:
LibDHCP::packOptions4(buf, options_);
......@@ -141,7 +141,7 @@ Option::unpackOptions(const OptionBuffer& buf) {
}
}
uint16_t Option::len() {
uint16_t Option::len() const {
// Returns length of the complete option (data length + DHCPv4/DHCPv6
// option header)
......@@ -149,7 +149,7 @@ uint16_t Option::len() {
size_t length = getHeaderLen() + data_.size();
// ... and sum of lengths of all suboptions
for (OptionCollection::iterator it = options_.begin();
for (OptionCollection::const_iterator it = options_.begin();
it != options_.end();
++it) {
length += (*it).second->len();
......@@ -162,7 +162,7 @@ uint16_t Option::len() {
}
bool
Option::valid() {
Option::valid() const {
if (universe_ != V4 &&
universe_ != V6) {
return (false);
......@@ -171,7 +171,7 @@ Option::valid() {
return (true);
}
OptionPtr Option::getOption(uint16_t opt_type) {
OptionPtr Option::getOption(uint16_t opt_type) const {
isc::dhcp::OptionCollection::const_iterator x =
options_.find(opt_type);
if ( x != options_.end() ) {
......@@ -190,7 +190,7 @@ bool Option::delOption(uint16_t opt_type) {
}
std::string Option::toText(int indent) {
std::string Option::toText(int indent) const {
std::stringstream output;
output << headerToText(indent) << ": ";
......@@ -209,13 +209,13 @@ std::string Option::toText(int indent) {
}
std::string
Option::toString() {
Option::toString() const {
/// @todo: Implement actual conversion in derived classes.
return (toText(0));
}
std::vector<uint8_t>
Option::toBinary(const bool include_header) {
Option::toBinary(const bool include_header) const {
OutputBuffer buf(len());
try {
// If the option is too long, exception will be thrown. We allow
......@@ -236,7 +236,7 @@ Option::toBinary(const bool include_header) {
}
std::string
Option::toHexString(const bool include_header) {
Option::toHexString(const bool include_header) const {
// Prepare binary version of the option.
std::vector<uint8_t> option_vec = toBinary(include_header);
......@@ -250,7 +250,7 @@ Option::toHexString(const bool include_header) {
}
std::string
Option::headerToText(const int indent, const std::string& type_name) {
Option::headerToText(const int indent, const std::string& type_name) const {
std::stringstream output;
for (int i = 0; i < indent; i++)
output << " ";
......@@ -284,7 +284,7 @@ Option::suboptionsToText(const int indent) const {
}
uint16_t
Option::getHeaderLen() {
Option::getHeaderLen() const {
switch (universe_) {
case V4:
return OPTION4_HDR_LEN; // header length for v4
......@@ -305,7 +305,7 @@ void Option::addOption(OptionPtr opt) {
options_.insert(make_pair(opt->getType(), opt));
}
uint8_t Option::getUint8() {
uint8_t Option::getUint8() const {
if (data_.size() < sizeof(uint8_t) ) {
isc_throw(OutOfRange, "Attempt to read uint8 from option " << type_
<< " that has size " << data_.size());
......@@ -313,12 +313,12 @@ uint8_t Option::getUint8() {
return (data_[0]);
}
uint16_t Option::getUint16() {
uint16_t Option::getUint16() const {
// readUint16() checks and throws OutOfRange if data_ is too small.
return (readUint16(&data_[0], data_.size()));
}
uint32_t Option::getUint32() {
uint32_t Option::getUint32() const {
// readUint32() checks and throws OutOfRange if data_ is too small.
return (readUint32(&data_[0], data_.size()));
}
......
......@@ -158,7 +158,7 @@ public:
/// @param buf pointer to a buffer
///
/// @throw BadValue Universe of the option is neither V4 nor V6.
virtual void pack(isc::util::OutputBuffer& buf);
virtual void pack(isc::util::OutputBuffer& buf) const;
/// @brief Parses received buffer.
///
......@@ -172,7 +172,7 @@ public:
/// @param indent number of spaces before printing text
///
/// @return string with text representation.
virtual std::string toText(int indent = 0);
virtual std::string toText(int indent = 0) const;
/// @brief Returns string representation of the value
///
......@@ -180,7 +180,7 @@ public:
/// refers to a specific option.
///
/// @return string that represents the value of the option.
virtual std::string toString();
virtual std::string toString() const;
/// @brief Returns binary representation of the option.
///
......@@ -189,7 +189,7 @@ public:
/// header fields.
///
/// @return Vector holding binary representation of the option.
virtual std::vector<uint8_t> toBinary(const bool include_header = false);
virtual std::vector<uint8_t> toBinary(const bool include_header = false) const;
/// @brief Returns string containing hexadecimal representation of option.
///
......@@ -198,7 +198,7 @@ public:
/// header fields.
///
/// @return String containing hexadecimal representation of the option.
virtual std::string toHexString(const bool include_header = false);
virtual std::string toHexString(const bool include_header = false) const;
/// Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
///
......@@ -209,17 +209,17 @@ public:
/// option header)
///
/// @return length of the option
virtual uint16_t len();
virtual uint16_t len() const;
/// @brief Returns length of header (2 for v4, 4 for v6)
///
/// @return length of option header
virtual uint16_t getHeaderLen();
virtual uint16_t getHeaderLen() const;
/// returns if option is valid (e.g. option may be truncated)
///
/// @return true, if option is valid
virtual bool valid();
virtual bool valid() const;
/// Returns pointer to actual data.
///
......@@ -246,7 +246,7 @@ public:
/// @param type type of requested suboption
///
/// @return shared_ptr to requested suoption
OptionPtr getOption(uint16_t type);
OptionPtr getOption(uint16_t type) const;
/// @brief Returns all encapsulated options.
///
......@@ -269,21 +269,21 @@ public:
/// @throw isc::OutOfRange Thrown if the option has a length of 0.
///
/// @return value of the first byte
uint8_t getUint8();
uint8_t getUint8() const;
/// @brief Returns content of first word.
///
/// @throw isc::OutOfRange Thrown if the option has a length less than 2.
///
/// @return uint16_t value stored on first two bytes
uint16_t getUint16();
uint16_t getUint16() const;
/// @brief Returns content of first double word.
///
/// @throw isc::OutOfRange Thrown if the option has a length less than 4.
///
/// @return uint32_t value stored on first four bytes
uint32_t getUint32();
uint32_t getUint32() const;
/// @brief Sets content of this option to singe uint8 value.
///
......@@ -378,7 +378,7 @@ protected:
/// directly by other classes.
///
/// @param [out] buf output buffer.
void packHeader(isc::util::OutputBuffer& buf);
void packHeader(isc::util::OutputBuffer& buf) const;
/// @brief Store sub options in a buffer.
///
......@@ -393,7 +393,7 @@ protected:
/// exceptions thrown by pack methods invoked on objects
/// representing sub options. We should consider whether to aggregate
/// those into one exception which can be documented here.
void packOptions(isc::util::OutputBuffer& buf);
void packOptions(isc::util::OutputBuffer& buf) const;
/// @brief Builds a collection of sub options from the buffer.
///
......@@ -420,7 +420,7 @@ protected:
///
/// @return Option header in the textual format.
std::string headerToText(const int indent = 0,
const std::string& type_name = "");
const std::string& type_name = "") const;
/// @brief Returns collection of suboptions in the textual format.
///
......@@ -441,7 +441,7 @@ protected:
/// It is used in constructors. In there are any problems detected
/// (like specifying type > 255 for DHCPv4 option), it will throw
/// BadValue or OutOfRange exceptions.
void check();
void check() const;
/// option universe (V4 or V6)
Universe universe_;
......
......@@ -56,7 +56,7 @@ Option4AddrLst::Option4AddrLst(uint8_t type, const IOAddress& addr)
}
void
Option4AddrLst::pack(isc::util::OutputBuffer& buf) {
Option4AddrLst::pack(isc::util::OutputBuffer& buf) const {
if (addrs_.size() * V4ADDRESS_LEN > 255) {
isc_throw(OutOfRange, "DHCPv4 Option4AddrLst " << type_ << " is too big."
......@@ -106,13 +106,13 @@ void Option4AddrLst::addAddress(const isc::asiolink::IOAddress& addr) {
addrs_.push_back(addr);
}
uint16_t Option4AddrLst::len() {
uint16_t Option4AddrLst::len() const {
// Returns length of the complete option (option header + data length)
return (getHeaderLen() + addrs_.size() * V4ADDRESS_LEN);
}
std::string Option4AddrLst::toText(int indent) {
std::string Option4AddrLst::toText(int indent) const {
std::stringstream output;
output << headerToText(indent) << ":";
......
......@@ -85,20 +85,20 @@ public:
/// Method will throw if option storing fails for some reason.
///
/// @param buf output buffer (option will be stored there)
virtual void pack(isc::util::OutputBuffer& buf);
virtual void pack(isc::util::OutputBuffer& buf) const;
/// Returns string representation of the option.
///
/// @param indent number of spaces before printing text
///
/// @return string with text representation.
virtual std::string toText(int indent = 0);
virtual std::string toText(int indent = 0) const;
/// Returns length of the complete option (data length + DHCPv4/DHCPv6
/// option header)
///
/// @return length of the option
virtual uint16_t len();
virtual uint16_t len() const;
/// @brief Returns vector with addresses.
///
......
......@@ -463,7 +463,7 @@ Option4ClientFqdn::getDomainNameType() const {
}
void
Option4ClientFqdn::pack(isc::util::OutputBuffer& buf) {
Option4ClientFqdn::pack(isc::util::OutputBuffer& buf) const {
// Header = option code and length.
packHeader(buf);
// Flags field.
......@@ -487,7 +487,7 @@ Option4ClientFqdn::unpack(OptionBufferConstIter first,
}
std::string
Option4ClientFqdn::toText(int indent) {
Option4ClientFqdn::toText(int indent) const {
std::ostringstream stream;
std::string in(indent, ' '); // base indentation
stream << in << "type=" << type_ << " (CLIENT_FQDN), "
......@@ -504,7 +504,7 @@ Option4ClientFqdn::toText(int indent) {
}
uint16_t
Option4ClientFqdn::len() {
Option4ClientFqdn::len() const {
uint16_t domain_name_length = 0;
// Try to calculate the length of the domain name only if there is
// any domain name specified.
......
......@@ -299,7 +299,7 @@ public:
/// @brief Writes option in the wire format into a buffer.
///
/// @param [out] buf output buffer where option data will be stored.
virtual void pack(isc::util::OutputBuffer& buf);
virtual void pack(isc::util::OutputBuffer& buf) const;
/// @brief Parses option from the received buffer.
///
......@@ -322,13 +322,13 @@ public:
/// @param indent number of spaces before printed text.
///
/// @return string with text representation.
virtual std::string toText(int indent = 0);
virtual std::string toText(int indent = 0) const;
/// @brief Returns length of the complete option (data length +
/// DHCPv4 option header).
///
/// @return length of the option.
virtual uint16_t len();
virtual uint16_t len() const;
///
/// @name Well known Rcode declarations for DHCPv4 Client FQDN %Option
......
......@@ -56,7 +56,7 @@ Option6AddrLst::setAddresses(const AddressContainer& addrs) {
addrs_ = addrs;
}
void Option6AddrLst::pack(isc::util::OutputBuffer& buf) {
void Option6AddrLst::pack(isc::util::OutputBuffer& buf) const {
buf.writeUint16(type_);
......@@ -89,7 +89,7 @@ void Option6AddrLst::unpack(OptionBufferConstIter begin,
}
}
std::string Option6AddrLst::toText(int indent) {
std::string Option6AddrLst::toText(int indent) const {
stringstream output;
output << headerToText(indent) << ":";
......@@ -100,7 +100,7 @@ std::string Option6AddrLst::toText(int indent) {
return (output.str());
}
uint16_t Option6AddrLst::len() {
uint16_t Option6AddrLst::len() const {
return (OPTION6_HDR_LEN + addrs_.size() * V6ADDRESS_LEN);
}
......
......@@ -48,7 +48,7 @@ public:
/// @brief Assembles on-wire form of this option
///
/// @param buf pointer to packet buffer
void pack(isc::util::OutputBuffer& buf);
void pack(isc::util::OutputBuffer& buf) const;
/// @brief Parses received data
///
......@@ -57,7 +57,7 @@ public:
virtual void unpack(OptionBufferConstIter begin,
OptionBufferConstIter end);
virtual std::string toText(int indent = 0);
virtual std::string toText(int indent = 0) const;
/// @brief Sets a single address.
///
......@@ -80,7 +80,7 @@ public:
AddressContainer getAddresses() const { return addrs_; };
// returns data length (data length + DHCPv4/DHCPv6 option header)
virtual uint16_t len();
virtual uint16_t len() const;
protected:
AddressContainer addrs_;
......
......@@ -395,7 +395,7 @@ Option6ClientFqdn::getDomainNameType() const {
}
void
Option6ClientFqdn::pack(isc::util::OutputBuffer& buf) {
Option6ClientFqdn::pack(isc::util::OutputBuffer& buf) const {
// Header = option code and length.
packHeader(buf);
// Flags field.
......@@ -416,7 +416,7 @@ Option6ClientFqdn::unpack(OptionBufferConstIter first,
}
std::string
Option6ClientFqdn::toText(int indent) {
Option6ClientFqdn::toText(int indent) const {
std::ostringstream stream;
std::string in(indent, ' '); // base indentation
stream << in << "type=" << type_ << "(CLIENT_FQDN)" << ", "
......@@ -432,7 +432,7 @@ Option6ClientFqdn::toText(int indent) {
}
uint16_t
Option6ClientFqdn::len() {
Option6ClientFqdn::len() const {
uint16_t domain_name_length = 0;
if (impl_->domain_name_) {
// If domain name is partial, the NULL terminating character
......
......@@ -217,7 +217,7 @@ public:
/// @brief Writes option in the wire format into a buffer.
///
/// @param [out] buf output buffer where option data will be stored.
virtual void pack(isc::util::OutputBuffer& buf);
virtual void pack(isc::util::OutputBuffer& buf) const;
/// @brief Parses option from the received buffer.
///
......@@ -240,13 +240,13 @@ public:
/// @param indent number of spaces before printed text.
///
/// @return string with text representation.
virtual std::string toText(int indent = 0);
virtual std::string toText(int indent = 0) const;
/// @brief Returns length of the complete option (data length +
/// DHCPv6 option header).
///
/// @return length of the option.
virtual uint16_t len();
virtual uint16_t len() const;
private:
......
......@@ -48,7 +48,7 @@ Option6IA::Option6IA(uint16_t type, OptionBufferConstIter begin,
unpack(begin, end);
}
void Option6IA::pack(isc::util::OutputBuffer& buf) {
void Option6IA::pack(isc::util::OutputBuffer& buf) const {
buf.writeUint16(type_);
buf.writeUint16(len() - OPTION6_HDR_LEN);
buf.writeUint32(iaid_);
......@@ -76,7 +76,7 @@ void Option6IA::unpack(OptionBufferConstIter begin,
unpackOptions(OptionBuffer(begin, end));
}
std::string Option6IA::toText(int indent) {
std::string Option6IA::toText(int indent) const {
stringstream output;
switch(getType()) {
......@@ -96,13 +96,13 @@ std::string Option6IA::toText(int indent) {
return (output.str());
}
uint16_t Option6IA::len() {
uint16_t Option6IA::len() const {
uint16_t length = OPTION6_HDR_LEN /*header (4)*/ +
OPTION6_IA_LEN /* option content (12) */;
// length of all suboptions
for (OptionCollection::iterator it = options_.begin();
for (OptionCollection::const_iterator it = options_.begin();
it != options_.end();
++it) {
length += (*it).second->len();
......
......@@ -43,7 +43,7 @@ public:
/// byte after stored option.
///
/// @param buf buffer (option will be stored here)
void pack(isc::util::OutputBuffer& buf);
void pack(isc::util::OutputBuffer& buf) const;
/// @brief Parses received buffer
///
......@@ -59,8 +59,7 @@ public:
/// @param indent number of leading space characters
///
/// @return string with text represenation
virtual std::string
toText(int indent = 0);
virtual std::string toText(int indent = 0) const;
/// Sets T1 timer.
///
......@@ -98,7 +97,7 @@ public:
/// Returns length of this option, including option header and suboptions
///
/// @return length of this option
virtual uint16_t len();
virtual uint16_t len() const;
protected:
......
......@@ -42,7 +42,7 @@ Option6IAAddr::Option6IAAddr(uint32_t type, OptionBuffer::const_iterator begin,
unpack(begin, end);
}
void Option6IAAddr::pack(isc::util::OutputBuffer& buf) {
void Option6IAAddr::pack(isc::util::OutputBuffer& buf) const {
buf.writeUint16(type_);
......@@ -81,7 +81,7 @@ void Option6IAAddr::unpack(OptionBuffer::const_iterator begin,
unpackOptions(OptionBuffer(begin, end));
}
std::string Option6IAAddr::toText(int indent) {
std::string Option6IAAddr::toText(int indent) const {
std::stringstream output;
output << headerToText(indent, "IAADDR") << ": "
<< "address=" << addr_
......@@ -92,14 +92,14 @@ std::string Option6IAAddr::toText(int indent) {
return (output.str());
}
uint16_t Option6IAAddr::len() {
uint16_t Option6IAAddr::len() const {
uint16_t length = OPTION6_HDR_LEN + OPTION6_IAADDR_LEN;
// length of all suboptions
// TODO implement:
// protected: unsigned short Option::lenHelper(int header_size);
for (OptionCollection::iterator it = options_.begin();
for (OptionCollection::const_iterator it = options_.begin();
it != options_.end();
++it) {
length += (*it).second->len();
......
......@@ -52,7 +52,7 @@ public:
/// byte after stored option.
///
/// @param buf pointer to a buffer
void pack(isc::util::OutputBuffer& buf);
void pack(isc::util::OutputBuffer& buf) const;
/// @brief Parses received buffer.
///
......@@ -67,7 +67,7 @@ public:
///
/// @return string with text representation.
virtual std::string
toText(int indent = 0);
toText(int indent = 0) const;
/// sets address in this option.
......@@ -106,7 +106,7 @@ public:
getValid() const { return valid_; }
/// returns data length (data length + DHCPv4/DHCPv6 option header)
virtual uint16_t len();
virtual uint16_t len() const;
protected:
/// contains an IPv6 address
......
......@@ -44,7 +44,7 @@ Option6IAPrefix::Option6IAPrefix(uint32_t type, OptionBuffer::const_iterator beg
unpack(begin, end);
}
void Option6IAPrefix::pack(isc::util::OutputBuffer& buf) {
void Option6IAPrefix::pack(isc::util::OutputBuffer& buf) const {
if (!addr_.isV6()) {
isc_throw(isc::BadValue, addr_ << " is not an IPv6 address");
}
......@@ -90,7 +90,7 @@ void Option6IAPrefix::unpack(OptionBuffer::const_iterator begin,
unpackOptions(OptionBuffer(begin, end));
}
std::string Option6IAPrefix::toText(int indent) {