Kea  2.3.8
option.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef OPTION_H
8 #define OPTION_H
9 
10 #include <util/buffer.h>
11 
12 #include <boost/shared_ptr.hpp>
13 
14 #include <map>
15 #include <string>
16 #include <vector>
17 
18 namespace isc {
19 namespace dhcp {
20 
24 typedef std::vector<uint8_t> OptionBuffer;
25 
27 typedef OptionBuffer::iterator OptionBufferIter;
28 
30 typedef OptionBuffer::const_iterator OptionBufferConstIter;
31 
33 typedef boost::shared_ptr<OptionBuffer> OptionBufferPtr;
34 
36 class Option;
37 typedef boost::shared_ptr<Option> OptionPtr;
38 
40 typedef std::multimap<unsigned int, OptionPtr> OptionCollection;
41 
43 typedef boost::shared_ptr<OptionCollection> OptionCollectionPtr;
44 
53 public:
54  SkipRemainingOptionsError (const char* file, size_t line, const char* what) :
55  isc::Exception(file, line, what) { };
56 };
57 
68 public:
69  SkipThisOptionError (const char* file, size_t line, const char* what) :
70  isc::Exception(file, line, what) { };
71 };
72 
73 
74 class Option {
75 public:
77  const static size_t OPTION4_HDR_LEN = 2;
78 
80  const static size_t OPTION6_HDR_LEN = 4;
81 
83  enum Universe { V4, V6 };
84 
85 
96  typedef OptionPtr Factory(Option::Universe u, uint16_t type, const OptionBuffer& buf);
97 
113  uint16_t type,
114  const OptionBuffer& buf);
115 
131  static OptionPtr factory(Option::Universe u, uint16_t type) {
132  return factory(u, type, OptionBuffer());
133  }
134 
139  Option(Universe u, uint16_t type);
140 
151  Option(Universe u, uint16_t type, const OptionBuffer& data);
152 
173  Option(Universe u, uint16_t type, OptionBufferConstIter first,
174  OptionBufferConstIter last);
175 
182  Option(const Option& source);
183 
195  static OptionPtr create(Universe u, uint16_t type);
196 
209  static OptionPtr create(Universe u, uint16_t type, const OptionBuffer& data);
210 
218  Option& operator=(const Option& rhs);
219 
228  virtual OptionPtr clone() const;
229 
234  return (universe_);
235  }
236 
248  virtual void pack(isc::util::OutputBuffer& buf, bool check = true) const;
249 
254  virtual void unpack(OptionBufferConstIter begin,
256 
262  virtual std::string toText(int indent = 0) const;
263 
270  virtual std::string toString() const;
271 
279  virtual std::vector<uint8_t> toBinary(const bool include_header = false) const;
280 
288  virtual std::string toHexString(const bool include_header = false) const;
289 
293  uint16_t getType() const {
294  return (type_);
295  }
296 
301  virtual uint16_t len() const;
302 
306  virtual uint16_t getHeaderLen() const;
307 
311  virtual bool valid() const;
312 
317  virtual const OptionBuffer& getData() const {
318  return (data_);
319  }
320 
333  void addOption(OptionPtr opt);
334 
340  OptionPtr getOption(uint16_t type) const;
341 
347  const OptionCollection& getOptions() const {
348  return (options_);
349  }
350 
358  return (options_);
359  }
360 
366  void getOptionsCopy(OptionCollection& options_copy) const;
367 
373  bool delOption(uint16_t type);
374 
380  uint8_t getUint8() const;
381 
387  uint16_t getUint16() const;
388 
394  uint32_t getUint32() const;
395 
401  void setUint8(uint8_t value);
402 
408  void setUint16(uint16_t value);
409 
415  void setUint32(uint32_t value);
416 
426  template<typename InputIterator>
427  void setData(InputIterator first, InputIterator last) {
428  data_.assign(first, last);
429  }
430 
435  void setEncapsulatedSpace(const std::string& encapsulated_space) {
436  encapsulated_space_ = encapsulated_space;
437  }
438 
442  std::string getEncapsulatedSpace() const {
443  return (encapsulated_space_);
444  }
445 
447  virtual ~Option();
448 
457  bool equals(const OptionPtr& other) const;
458 
471  virtual bool equals(const Option& other) const;
472 
483  static bool lenient_parsing_;
484 
485 protected:
486 
496  template<typename OptionType>
498  const OptionType* cast_this = dynamic_cast<const OptionType*>(this);
499  if (cast_this) {
500  return (boost::shared_ptr<OptionType>(new OptionType(*cast_this)));
501  }
502  return (OptionPtr());
503  }
504 
520  void packHeader(isc::util::OutputBuffer& buf, bool check = true) const;
521 
536  void packOptions(isc::util::OutputBuffer& buf, bool check = true) const;
537 
550  void unpackOptions(const OptionBuffer& buf);
551 
562  std::string headerToText(const int indent = 0,
563  const std::string& type_name = "") const;
564 
577  std::string suboptionsToText(const int indent = 0) const;
578 
584  void check() const;
585 
588 
590  uint16_t type_;
591 
594 
597 
599  std::string encapsulated_space_;
600 
603 };
604 
605 } // namespace isc::dhcp
606 } // namespace isc
607 
608 #endif // OPTION_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static OptionPtr factory(Option::Universe u, uint16_t type, const OptionBuffer &buf)
Factory function to create instance of option.
Definition: option.cc:33
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:590
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:288
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition: option.cc:307
static bool lenient_parsing_
Governs whether options should be parsed less strictly.
Definition: option.h:483
std::string encapsulated_space_
Name of the option space being encapsulated by this option.
Definition: option.h:599
OptionCollection & getMutableOptions()
Returns all encapsulated options.
Definition: option.h:357
std::string getEncapsulatedSpace() const
Returns the name of the option space encapsulated by this option.
Definition: option.h:442
bool equals(const OptionPtr &other) const
Checks if options are equal.
Definition: option.cc:374
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition: option.h:435
virtual ~Option()
just to force that every option has virtual dtor
Definition: option.cc:383
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in wire-format to a buffer.
Definition: option.cc:107
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
Definition: option.cc:219
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:321
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header)
Definition: option.cc:171
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:83
Universe universe_
option universe (V4 or V6)
Definition: option.h:587
OptionPtr getOption(uint16_t type) const
Returns shared_ptr to suboption of specific type.
Definition: option.cc:199
void addOption(OptionPtr opt)
Adds a sub-option.
Definition: option.cc:331
void setUint32(uint32_t value)
Sets content of this option to a single uint32 value.
Definition: option.cc:369
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:293
OptionBuffer data_
contains content of this data
Definition: option.h:593
virtual std::string toString() const
Returns string representation of the value.
Definition: option.cc:247
void unpackOptions(const OptionBuffer &buf)
Builds a collection of sub options from the buffer.
Definition: option.cc:155
void packOptions(isc::util::OutputBuffer &buf, bool check=true) const
Store sub options in a buffer.
Definition: option.cc:136
static OptionPtr create(Universe u, uint16_t type)
Factory function creating an instance of the Option.
Definition: option.cc:63
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:80
OptionPtr Factory(Option::Universe u, uint16_t type, const OptionBuffer &buf)
a factory function prototype
Definition: option.h:96
void setUint8(uint8_t value)
Sets content of this option to a single uint8 value.
Definition: option.cc:359
Option & operator=(const Option &rhs)
Assignment operator.
Definition: option.cc:73
const OptionCollection & getOptions() const
Returns all encapsulated options.
Definition: option.h:347
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition: option.h:427
virtual bool valid() const
returns if option is valid (e.g.
Definition: option.cc:190
OptionCollection options_
collection for storing suboptions
Definition: option.h:596
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition: option.h:497
Universe getUniverse() const
returns option universe (V4 or V6)
Definition: option.h:233
uint8_t getUint8() const
Returns content of first byte.
Definition: option.cc:341
virtual std::vector< uint8_t > toBinary(const bool include_header=false) const
Returns binary representation of the option.
Definition: option.cc:253
void setUint16(uint16_t value)
Sets content of this option to a single uint16 value.
Definition: option.cc:364
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option.cc:85
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition: option.cc:149
static OptionPtr factory(Option::Universe u, uint16_t type)
Factory function to create instance of option.
Definition: option.h:131
uint16_t getUint16() const
Returns content of first word.
Definition: option.cc:349
virtual std::string toText(int indent=0) const
Returns string representation of the option.
Definition: option.cc:228
void packHeader(isc::util::OutputBuffer &buf, bool check=true) const
Store option's header in a buffer.
Definition: option.cc:119
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition: option.h:317
virtual std::string toHexString(const bool include_header=false) const
Returns string containing hexadecimal representation of option.
Definition: option.cc:274
uint32_t getUint32() const
Returns content of first double word.
Definition: option.cc:354
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
Definition: option.cc:39
void check() const
A protected method used for option correctness.
Definition: option.cc:90
void getOptionsCopy(OptionCollection &options_copy) const
Performs deep copy of suboptions.
Definition: option.cc:208
static const size_t OPTION4_HDR_LEN
length of the usual DHCPv4 option header (there are exceptions)
Definition: option.h:77
Exception thrown during option unpacking This exception is thrown when an error has occurred,...
Definition: option.h:52
SkipRemainingOptionsError(const char *file, size_t line, const char *what)
Definition: option.h:54
Exception thrown during option unpacking This exception is thrown when an error has occurred unpackin...
Definition: option.h:67
SkipThisOptionError(const char *file, size_t line, const char *what)
Definition: option.h:69
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
boost::shared_ptr< OptionCollection > OptionCollectionPtr
A pointer to an OptionCollection.
Definition: option.h:43
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition: option.h:40
boost::shared_ptr< OptionBuffer > OptionBufferPtr
pointer to a DHCP buffer
Definition: option.h:33
OptionBuffer::iterator OptionBufferIter
iterator for walking over OptionBuffer
Definition: option.h:27
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
Defines the logger used by the top-level component of kea-lfc.