Kea  2.3.2-git
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 
112  static OptionPtr factory(Option::Universe u,
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,
255  OptionBufferConstIter end);
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 
357  OptionCollection& getMutableOptions() {
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>
497  OptionPtr cloneInternal() const {
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 
593  OptionBuffer data_;
594 
596  OptionCollection options_;
597 
599  std::string encapsulated_space_;
600 
603 };
604 
605 } // namespace isc::dhcp
606 } // namespace isc
607 
608 #endif // OPTION_H
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:293
Exception thrown during option unpacking This exception is thrown when an error has occurred...
Definition: option.h:52
boost::shared_ptr< OptionBuffer > OptionBufferPtr
pointer to a DHCP buffer
Definition: option.h:33
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:83
static OptionPtr factory(Option::Universe u, uint16_t type)
Factory function to create instance of option.
Definition: option.h:131
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition: option.h:497
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Universe universe_
option universe (V4 or V6)
Definition: option.h:587
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
const OptionCollection & getOptions() const
Returns all encapsulated options.
Definition: option.h:347
Universe getUniverse() const
returns option universe (V4 or V6)
Definition: option.h:233
OptionCollection options_
collection for storing suboptions
Definition: option.h:596
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition: option.h:317
SkipThisOptionError(const char *file, size_t line, const char *what)
Definition: option.h:69
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition: option.h:435
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition: option.h:40
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
std::string getEncapsulatedSpace() const
Returns the name of the option space encapsulated by this option.
Definition: option.h:442
OptionBuffer::iterator OptionBufferIter
iterator for walking over OptionBuffer
Definition: option.h:27
Exception thrown during option unpacking This exception is thrown when an error has occurred unpackin...
Definition: option.h:67
OptionBuffer data_
contains content of this data
Definition: option.h:593
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:590
OptionCollection & getMutableOptions()
Returns all encapsulated options.
Definition: option.h:357
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition: option.h:427
static bool lenient_parsing_
Governs whether options should be parsed less strictly.
Definition: option.h:483
SkipRemainingOptionsError(const char *file, size_t line, const char *what)
Definition: option.h:54
boost::shared_ptr< OptionCollection > OptionCollectionPtr
A pointer to an OptionCollection.
Definition: option.h:43
std::string encapsulated_space_
Name of the option space being encapsulated by this option.
Definition: option.h:599
OptionPtr Factory(Option::Universe u, uint16_t type, const OptionBuffer &buf)
a factory function prototype
Definition: option.h:96