/usr/include/kea/dhcp/option.h is in kea-dev 1.1.0-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | // Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef OPTION_H
#define OPTION_H
#include <util/buffer.h>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <map>
#include <string>
#include <vector>
namespace isc {
namespace dhcp {
/// @brief buffer types used in DHCP code.
///
/// Dereferencing OptionBuffer iterator will point out to contiguous memory.
typedef std::vector<uint8_t> OptionBuffer;
/// iterator for walking over OptionBuffer
typedef OptionBuffer::iterator OptionBufferIter;
/// const_iterator for walking over OptionBuffer
typedef OptionBuffer::const_iterator OptionBufferConstIter;
/// pointer to a DHCP buffer
typedef boost::shared_ptr<OptionBuffer> OptionBufferPtr;
/// shared pointer to Option object
class Option;
typedef boost::shared_ptr<Option> OptionPtr;
/// A collection of DHCP (v4 or v6) options
typedef std::multimap<unsigned int, OptionPtr> OptionCollection;
/// A pointer to an OptionCollection
typedef boost::shared_ptr<OptionCollection> OptionCollectionPtr;
class Option {
public:
/// length of the usual DHCPv4 option header (there are exceptions)
const static size_t OPTION4_HDR_LEN = 2;
/// length of any DHCPv6 option header
const static size_t OPTION6_HDR_LEN = 4;
/// defines option universe DHCPv4 or DHCPv6
enum Universe { V4, V6 };
/// @brief a factory function prototype
///
/// @param u option universe (DHCPv4 or DHCPv6)
/// @param type option type
/// @param buf pointer to a buffer
///
/// @todo Passing a separate buffer for each option means that a copy
/// was done. We can avoid it by passing 2 iterators.
///
/// @return a pointer to a created option object
typedef OptionPtr Factory(Option::Universe u, uint16_t type, const OptionBuffer& buf);
/// @brief Factory function to create instance of option.
///
/// Factory method creates instance of specified option. The option
/// to be created has to have corresponding factory function
/// registered with \ref LibDHCP::OptionFactoryRegister.
///
/// @param u universe of the option (V4 or V6)
/// @param type option-type
/// @param buf option-buffer
///
/// @return instance of option.
///
/// @throw isc::InvalidOperation if there is no factory function
/// registered for specified option type.
static OptionPtr factory(Option::Universe u,
uint16_t type,
const OptionBuffer& buf);
/// @brief Factory function to create instance of option.
///
/// Factory method creates instance of specified option. The option
/// to be created has to have corresponding factory function
/// registered with \ref LibDHCP::OptionFactoryRegister.
/// This method creates empty \ref OptionBuffer object. Use this
/// factory function if it is not needed to pass custom buffer.
///
/// @param u universe of the option (V4 or V6)
/// @param type option-type
///
/// @return instance of option.
///
/// @throw isc::InvalidOperation if there is no factory function
/// registered for specified option type.
static OptionPtr factory(Option::Universe u, uint16_t type) {
return factory(u, type, OptionBuffer());
}
/// @brief ctor, used for options constructed, usually during transmission
///
/// @param u option universe (DHCPv4 or DHCPv6)
/// @param type option type
Option(Universe u, uint16_t type);
/// @brief Constructor, used for received options.
///
/// This constructor takes vector<uint8_t>& which is used in cases
/// when content of the option will be copied and stored within
/// option object. V4 Options follow that approach already.
/// @todo Migrate V6 options to that approach.
///
/// @param u specifies universe (V4 or V6)
/// @param type option type (0-255 for V4 and 0-65535 for V6)
/// @param data content of the option
Option(Universe u, uint16_t type, const OptionBuffer& data);
/// @brief Constructor, used for received options.
///
/// This constructor is similar to the previous one, but it does not take
/// the whole vector<uint8_t>, but rather subset of it.
///
/// @todo This can be templated to use different containers, not just
/// vector. Prototype should look like this:
/// template<typename InputIterator> Option(Universe u, uint16_t type,
/// InputIterator first, InputIterator last);
///
/// vector<int8_t> myData;
/// Example usage: new Option(V4, 123, myData.begin()+1, myData.end()-1)
/// This will create DHCPv4 option of type 123 that contains data from
/// trimmed (first and last byte removed) myData vector.
///
/// @param u specifies universe (V4 or V6)
/// @param type option type (0-255 for V4 and 0-65535 for V6)
/// @param first iterator to the first element that should be copied
/// @param last iterator to the next element after the last one
/// to be copied.
Option(Universe u, uint16_t type, OptionBufferConstIter first,
OptionBufferConstIter last);
/// @brief Copy constructor.
///
/// This constructor makes a deep copy of the option and all of the
/// suboptions. It calls @ref getOptionsCopy to deep copy suboptions.
///
/// @param source Option to be copied.
Option(const Option& source);
/// @brief Assignment operator.
///
/// The assignment operator performs a deep copy of the option and
/// its suboptions. It calls @ref getOptionsCopy to deep copy
/// suboptions.
///
/// @param rhs Option to be assigned.
Option& operator=(const Option& rhs);
/// @brief Copies this option and returns a pointer to the copy.
///
/// This function must be overridden in the derived classes to make
/// a copy of the derived type. The simplest way to do it is by
/// calling @ref cloneInternal function with an appropriate template
/// parameter.
///
/// @return Pointer to the copy of the option.
virtual OptionPtr clone() const;
/// @brief returns option universe (V4 or V6)
///
/// @return universe type
Universe getUniverse() const { return universe_; };
/// @brief Writes option in wire-format to a buffer.
///
/// Writes option in wire-format to buffer, returns pointer to first unused
/// byte after stored option (that is useful for writing options one after
/// another).
///
/// @param buf pointer to a buffer
///
/// @throw BadValue Universe of the option is neither V4 nor V6.
virtual void pack(isc::util::OutputBuffer& buf) const;
/// @brief Parses received buffer.
///
/// @param begin iterator to first byte of option data
/// @param end iterator to end of option data (first byte after option end)
virtual void unpack(OptionBufferConstIter begin,
OptionBufferConstIter end);
/// 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) const;
/// @brief Returns string representation of the value
///
/// This is terse representation used in cases where client classification
/// refers to a specific option.
///
/// @return string that represents the value of the option.
virtual std::string toString() const;
/// @brief Returns binary representation of the option.
///
/// @param include_header Boolean flag which indicates if the output should
/// also contain header fields. The default is that it shouldn't include
/// header fields.
///
/// @return Vector holding binary representation of the option.
virtual std::vector<uint8_t> toBinary(const bool include_header = false) const;
/// @brief Returns string containing hexadecimal representation of option.
///
/// @param include_header Boolean flag which indicates if the output should
/// also contain header fields. The default is that it shouldn't include
/// header fields.
///
/// @return String containing hexadecimal representation of the option.
virtual std::string toHexString(const bool include_header = false) const;
/// Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
///
/// @return option type
uint16_t getType() const { return (type_); }
/// Returns length of the complete option (data length + DHCPv4/DHCPv6
/// option header)
///
/// @return length of the option
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() const;
/// returns if option is valid (e.g. option may be truncated)
///
/// @return true, if option is valid
virtual bool valid() const;
/// Returns pointer to actual data.
///
/// @return pointer to actual data (or reference to an empty vector
/// if there is no data)
virtual const OptionBuffer& getData() const { return (data_); }
/// Adds a sub-option.
///
/// Some DHCPv6 options can have suboptions. This method allows adding
/// options within options.
///
/// Note: option is passed by value. That is very convenient as it allows
/// downcasting from any derived classes, e.g. shared_ptr<Option6_IA> type
/// can be passed directly, without any casts. That would not be possible
/// with passing by reference. addOption() is expected to be used in
/// many places. Requiring casting is not feasible.
///
/// @param opt shared pointer to a suboption that is going to be added.
void addOption(OptionPtr opt);
/// Returns shared_ptr to suboption of specific type
///
/// @param type type of requested suboption
///
/// @return shared_ptr to requested suboption
OptionPtr getOption(uint16_t type) const;
/// @brief Returns all encapsulated options.
///
/// @warning This function returns a reference to the container holding
/// encapsulated options, which is valid as long as the object which
/// returned it exists.
const OptionCollection& getOptions() const {
return (options_);
}
/// @brief Performs deep copy of suboptions.
///
/// This method calls @ref clone method to deep copy each option.
///
/// @param [out] options_copy Container where copied options are stored.
void getOptionsCopy(OptionCollection& options_copy) const;
/// Attempts to delete first suboption of requested type
///
/// @param type Type of option to be deleted.
///
/// @return true if option was deleted, false if no such option existed
bool delOption(uint16_t type);
/// @brief Returns content of first byte.
///
/// @throw isc::OutOfRange Thrown if the option has a length of 0.
///
/// @return value of the first byte
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() 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() const;
/// @brief Sets content of this option to singe uint8 value.
///
/// Option it resized appropriately (to length of 1 octet).
///
/// @param value value to be set
void setUint8(uint8_t value);
/// @brief Sets content of this option to singe uint16 value.
///
/// Option it resized appropriately (to length of 2 octets).
///
/// @param value value to be set
void setUint16(uint16_t value);
/// @brief Sets content of this option to singe uint32 value.
///
/// Option it resized appropriately (to length of 4 octets).
///
/// @param value value to be set
void setUint32(uint32_t value);
/// @brief Sets content of this option from buffer.
///
/// Option will be resized to length of buffer.
///
/// @param first iterator pointing to beginning of buffer to copy.
/// @param last iterator pointing to end of buffer to copy.
///
/// @tparam InputIterator type of the iterator representing the
/// limits of the buffer to be assigned to a data_ buffer.
template<typename InputIterator>
void setData(InputIterator first, InputIterator last) {
data_.assign(first, last);
}
/// @brief Sets the name of the option space encapsulated by this option.
///
/// @param encapsulated_space name of the option space encapsulated by
/// this option.
void setEncapsulatedSpace(const std::string& encapsulated_space) {
encapsulated_space_ = encapsulated_space;
}
/// @brief Returns the name of the option space encapsulated by this option.
///
/// @return name of the option space encapsulated by this option.
std::string getEncapsulatedSpace() const {
return (encapsulated_space_);
}
/// just to force that every option has virtual dtor
virtual ~Option();
/// @brief Checks if options are equal.
///
/// This method calls a virtual @c equals function to compare objects.
/// This method is not meant to be overridden in the derived classes.
/// Instead, the other @c equals function must be overridden.
///
/// @param other Pointer to the option to compare this option to.
/// @return true if both options are equal, false otherwise.
bool equals(const OptionPtr& other) const;
/// @brief Checks if two options are equal.
///
/// Equality verifies option type and option content. Care should
/// be taken when using this method. Implementation for derived
/// classes should be provided when this method is expected to be
/// used. It is safe in general, as the first check (different types)
/// will detect differences between base Option and derived
/// objects.
///
/// @param other Instance of the option to compare to.
///
/// @return true if options are equal, false otherwise.
virtual bool equals(const Option& other) const;
protected:
/// @brief Copies this option and returns a pointer to the copy.
///
/// The deep copy of the option is performed by calling copy
/// constructor of the option of a given type. Derived classes call
/// this method in the implementations of @ref clone methods to
/// create a copy of the option of their type.
///
/// @tparam OptionType Type of the option of which a clone should
/// be created.
template<typename OptionType>
OptionPtr cloneInternal() const {
const OptionType* cast_this = dynamic_cast<const OptionType*>(this);
if (cast_this) {
return (boost::shared_ptr<OptionType>(new OptionType(*cast_this)));
}
return (OptionPtr());
}
/// @brief Store option's header in a buffer.
///
/// This method writes option's header into a buffer in the
/// on-wire format. The universe set for the particular option
/// is used to determine whether option code and length are
/// stored as 2-byte (for DHCPv6) or single-byte (for DHCPv4)
/// values. For DHCPv4 options, this method checks if the
/// length does not exceed 255 bytes and throws exception if
/// it does.
/// This method is used by derived classes to pack option's
/// header into a buffer. This method should not be called
/// directly by other classes.
///
/// @param [out] buf output buffer.
void packHeader(isc::util::OutputBuffer& buf) const;
/// @brief Store sub options in a buffer.
///
/// This method stores all sub-options defined for a particular
/// option in a on-wire format in output buffer provided.
/// This function is called by pack function in this class or
/// derived classes that override pack.
///
/// @param [out] buf output buffer.
///
/// @todo The set of exceptions thrown by this function depend on
/// 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) const;
/// @brief Builds a collection of sub options from the buffer.
///
/// This method parses the provided buffer and builds a collection
/// of objects representing sub options. This function may throw
/// different exceptions when option assembly fails.
///
/// @param buf buffer to be parsed.
///
/// @todo The set of exceptions thrown by this function depend on
/// exceptions thrown by unpack methods invoked on objects
/// representing sub options. We should consider whether to aggregate
/// those into one exception which can be documented here.
void unpackOptions(const OptionBuffer& buf);
/// @brief Returns option header in the textual format.
///
/// This protected method should be called by the derived classes in
/// their respective @c toText implementations.
///
/// @param indent Number of spaces to insert before the text.
/// @param type_name Option type name. If empty, the option name
/// is omitted.
///
/// @return Option header in the textual format.
std::string headerToText(const int indent = 0,
const std::string& type_name = "") const;
/// @brief Returns collection of suboptions in the textual format.
///
/// This protected method should be called by the derived classes
/// in their respective @c toText implementations to append the
/// suboptions held by this option. Note that there are some
/// option types which don't have suboptions because they contain
/// variable length fields. For such options this method is not
/// called.
///
/// @param indent Number of spaces to insert before the text.
///
//// @return Suboptions in the textual format.
std::string suboptionsToText(const int indent = 0) const;
/// @brief A protected method used for option correctness.
///
/// 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() const;
/// option universe (V4 or V6)
Universe universe_;
/// option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
uint16_t type_;
/// contains content of this data
OptionBuffer data_;
/// collection for storing suboptions
OptionCollection options_;
/// Name of the option space being encapsulated by this option.
std::string encapsulated_space_;
/// @todo probably 2 different containers have to be used for v4 (unique
/// options) and v6 (options with the same type can repeat)
};
} // namespace isc::dhcp
} // namespace isc
#endif // OPTION_H
|