Kea  1.9.9-git
option.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2021 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;
42 typedef boost::shared_ptr<OptionCollection> OptionCollectionPtr;
43 
52 public:
53  SkipRemainingOptionsError (const char* file, size_t line, const char* what) :
54  isc::Exception(file, line, what) { };
55 };
56 
67 public:
68  SkipThisOptionError (const char* file, size_t line, const char* what) :
69  isc::Exception(file, line, what) { };
70 };
71 
72 
73 class Option {
74 public:
76  const static size_t OPTION4_HDR_LEN = 2;
77 
79  const static size_t OPTION6_HDR_LEN = 4;
80 
82  enum Universe { V4, V6 };
83 
84 
95  typedef OptionPtr Factory(Option::Universe u, uint16_t type, const OptionBuffer& buf);
96 
111  static OptionPtr factory(Option::Universe u,
112  uint16_t type,
113  const OptionBuffer& buf);
114 
130  static OptionPtr factory(Option::Universe u, uint16_t type) {
131  return factory(u, type, OptionBuffer());
132  }
133 
138  Option(Universe u, uint16_t type);
139 
150  Option(Universe u, uint16_t type, const OptionBuffer& data);
151 
172  Option(Universe u, uint16_t type, OptionBufferConstIter first,
173  OptionBufferConstIter last);
174 
181  Option(const Option& source);
182 
194  static OptionPtr create(Universe u, uint16_t type);
195 
208  static OptionPtr create(Universe u, uint16_t type, const OptionBuffer& data);
209 
217  Option& operator=(const Option& rhs);
218 
227  virtual OptionPtr clone() const;
228 
232  Universe getUniverse() const { return universe_; };
233 
243  virtual void pack(isc::util::OutputBuffer& buf) const;
244 
249  virtual void unpack(OptionBufferConstIter begin,
250  OptionBufferConstIter end);
251 
257  virtual std::string toText(int indent = 0) const;
258 
265  virtual std::string toString() const;
266 
274  virtual std::vector<uint8_t> toBinary(const bool include_header = false) const;
275 
283  virtual std::string toHexString(const bool include_header = false) const;
284 
288  uint16_t getType() const { return (type_); }
289 
294  virtual uint16_t len() const;
295 
299  virtual uint16_t getHeaderLen() const;
300 
304  virtual bool valid() const;
305 
310  virtual const OptionBuffer& getData() const { return (data_); }
311 
324  void addOption(OptionPtr opt);
325 
331  OptionPtr getOption(uint16_t type) const;
332 
338  const OptionCollection& getOptions() const {
339  return (options_);
340  }
341 
347  void getOptionsCopy(OptionCollection& options_copy) const;
348 
354  bool delOption(uint16_t type);
355 
361  uint8_t getUint8() const;
362 
368  uint16_t getUint16() const;
369 
375  uint32_t getUint32() const;
376 
382  void setUint8(uint8_t value);
383 
389  void setUint16(uint16_t value);
390 
396  void setUint32(uint32_t value);
397 
407  template<typename InputIterator>
408  void setData(InputIterator first, InputIterator last) {
409  data_.assign(first, last);
410  }
411 
416  void setEncapsulatedSpace(const std::string& encapsulated_space) {
417  encapsulated_space_ = encapsulated_space;
418  }
419 
423  std::string getEncapsulatedSpace() const {
424  return (encapsulated_space_);
425  }
426 
428  virtual ~Option();
429 
438  bool equals(const OptionPtr& other) const;
439 
452  virtual bool equals(const Option& other) const;
453 
464  static bool lenient_parsing_;
465 
466 protected:
467 
477  template<typename OptionType>
478  OptionPtr cloneInternal() const {
479  const OptionType* cast_this = dynamic_cast<const OptionType*>(this);
480  if (cast_this) {
481  return (boost::shared_ptr<OptionType>(new OptionType(*cast_this)));
482  }
483  return (OptionPtr());
484  }
485 
500  void packHeader(isc::util::OutputBuffer& buf) const;
501 
515  void packOptions(isc::util::OutputBuffer& buf) const;
516 
529  void unpackOptions(const OptionBuffer& buf);
530 
541  std::string headerToText(const int indent = 0,
542  const std::string& type_name = "") const;
543 
556  std::string suboptionsToText(const int indent = 0) const;
557 
563  void check() const;
564 
567 
569  uint16_t type_;
570 
572  OptionBuffer data_;
573 
575  OptionCollection options_;
576 
578  std::string encapsulated_space_;
579 
582 };
583 
584 } // namespace isc::dhcp
585 } // namespace isc
586 
587 #endif // OPTION_H
Universe getUniverse() const
returns option universe (V4 or V6)
Definition: option.h:232
const OptionCollection & getOptions() const
Returns all encapsulated options.
Definition: option.h:338
void packHeader(isc::util::OutputBuffer &buf) const
Store option's header in a buffer.
Definition: option.cc:126
void packOptions(isc::util::OutputBuffer &buf) const
Store sub options in a buffer.
Definition: option.cc:146
virtual std::string toHexString(const bool include_header=false) const
Returns string containing hexadecimal representation of option.
Definition: option.cc:290
Exception thrown during option unpacking This exception is thrown when an error has occurred...
Definition: option.h:51
virtual ~Option()
just to force that every option has virtual dtor
Definition: option.cc:401
OptionPtr getOption(uint16_t type) const
Returns shared_ptr to suboption of specific type.
Definition: option.cc:211
bool equals(const OptionPtr &other) const
Checks if options are equal.
Definition: option.cc:392
virtual std::vector< uint8_t > toBinary(const bool include_header=false) const
Returns binary representation of the option.
Definition: option.cc:269
virtual void pack(isc::util::OutputBuffer &buf) const
Writes option in wire-format to a buffer.
Definition: option.cc:114
boost::shared_ptr< OptionBuffer > OptionBufferPtr
pointer to a DHCP buffer
Definition: option.h:33
void setUint16(uint16_t value)
Sets content of this option to a single uint16 value.
Definition: option.cc:382
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:82
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition: option.h:310
static OptionPtr factory(Option::Universe u, uint16_t type)
Factory function to create instance of option.
Definition: option.h:130
virtual std::string toString() const
Returns string representation of the value.
Definition: option.cc:263
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
Definition: option.cc:40
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:338
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition: option.h:478
Universe universe_
option universe (V4 or V6)
Definition: option.h:566
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
static const size_t OPTION4_HDR_LEN
length of the usual DHCPv4 option header (there are exceptions)
Definition: option.h:76
void setUint32(uint32_t value)
Sets content of this option to a single uint32 value.
Definition: option.cc:387
uint8_t getUint8() const
Returns content of first byte.
Definition: option.cc:359
void check() const
A protected method used for option correctness.
Definition: option.cc:91
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
Definition: option.cc:234
OptionCollection options_
collection for storing suboptions
Definition: option.h:575
SkipThisOptionError(const char *file, size_t line, const char *what)
Definition: option.h:68
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition: option.h:416
virtual std::string toText(int indent=0) const
Returns string representation of the option.
Definition: option.cc:244
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition: option.h:40
void addOption(OptionPtr opt)
Adds a sub-option.
Definition: option.cc:348
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option.cc:86
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition: option.cc:323
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
Option & operator=(const Option &rhs)
Assignment operator.
Definition: option.cc:74
static OptionPtr factory(Option::Universe u, uint16_t type, const OptionBuffer &buf)
Factory function to create instance of option.
Definition: option.cc:33
void unpackOptions(const OptionBuffer &buf)
Builds a collection of sub options from the buffer.
Definition: option.cc:165
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:288
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:304
static OptionPtr create(Universe u, uint16_t type)
Factory function creating an instance of the Option.
Definition: option.cc:64
OptionBuffer::iterator OptionBufferIter
iterator for walking over OptionBuffer
Definition: option.h:27
std::string getEncapsulatedSpace() const
Returns the name of the option space encapsulated by this option.
Definition: option.h:423
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition: option.cc:159
Exception thrown during option unpacking This exception is thrown when an error has occurred unpackin...
Definition: option.h:66
void setUint8(uint8_t value)
Sets content of this option to a single uint8 value.
Definition: option.cc:377
uint32_t getUint32() const
Returns content of first double word.
Definition: option.cc:372
OptionBuffer data_
contains content of this data
Definition: option.h:572
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:569
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition: option.h:408
void getOptionsCopy(OptionCollection &options_copy) const
Performs deep copy of suboptions.
Definition: option.cc:221
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header) ...
Definition: option.cc:181
uint16_t getUint16() const
Returns content of first word.
Definition: option.cc:367
static bool lenient_parsing_
Governs whether options should be parsed less strictly.
Definition: option.h:464
SkipRemainingOptionsError(const char *file, size_t line, const char *what)
Definition: option.h:53
boost::shared_ptr< OptionCollection > OptionCollectionPtr
A pointer to an OptionCollection.
Definition: option.h:42
std::string encapsulated_space_
Name of the option space being encapsulated by this option.
Definition: option.h:578
OptionPtr Factory(Option::Universe u, uint16_t type, const OptionBuffer &buf)
a factory function prototype
Definition: option.h:95
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:79
virtual bool valid() const
returns if option is valid (e.g.
Definition: option.cc:202