Kea  1.5.0
option.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2018 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/function.hpp>
13 #include <boost/shared_ptr.hpp>
14 
15 #include <map>
16 #include <string>
17 #include <vector>
18 
19 namespace isc {
20 namespace dhcp {
21 
25 typedef std::vector<uint8_t> OptionBuffer;
26 
28 typedef OptionBuffer::iterator OptionBufferIter;
29 
31 typedef OptionBuffer::const_iterator OptionBufferConstIter;
32 
34 typedef boost::shared_ptr<OptionBuffer> OptionBufferPtr;
35 
37 class Option;
38 typedef boost::shared_ptr<Option> OptionPtr;
39 
41 typedef std::multimap<unsigned int, OptionPtr> OptionCollection;
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 
58 class Option {
59 public:
61  const static size_t OPTION4_HDR_LEN = 2;
62 
64  const static size_t OPTION6_HDR_LEN = 4;
65 
67  enum Universe { V4, V6 };
68 
69 
80  typedef OptionPtr Factory(Option::Universe u, uint16_t type, const OptionBuffer& buf);
81 
97  uint16_t type,
98  const OptionBuffer& buf);
99 
115  static OptionPtr factory(Option::Universe u, uint16_t type) {
116  return factory(u, type, OptionBuffer());
117  }
118 
123  Option(Universe u, uint16_t type);
124 
135  Option(Universe u, uint16_t type, const OptionBuffer& data);
136 
157  Option(Universe u, uint16_t type, OptionBufferConstIter first,
158  OptionBufferConstIter last);
159 
166  Option(const Option& source);
167 
175  Option& operator=(const Option& rhs);
176 
185  virtual OptionPtr clone() const;
186 
190  Universe getUniverse() const { return universe_; };
191 
201  virtual void pack(isc::util::OutputBuffer& buf) const;
202 
207  virtual void unpack(OptionBufferConstIter begin,
209 
215  virtual std::string toText(int indent = 0) const;
216 
223  virtual std::string toString() const;
224 
232  virtual std::vector<uint8_t> toBinary(const bool include_header = false) const;
233 
241  virtual std::string toHexString(const bool include_header = false) const;
242 
246  uint16_t getType() const { return (type_); }
247 
252  virtual uint16_t len() const;
253 
257  virtual uint16_t getHeaderLen() const;
258 
262  virtual bool valid() const;
263 
268  virtual const OptionBuffer& getData() const { return (data_); }
269 
282  void addOption(OptionPtr opt);
283 
289  OptionPtr getOption(uint16_t type) const;
290 
296  const OptionCollection& getOptions() const {
297  return (options_);
298  }
299 
305  void getOptionsCopy(OptionCollection& options_copy) const;
306 
312  bool delOption(uint16_t type);
313 
319  uint8_t getUint8() const;
320 
326  uint16_t getUint16() const;
327 
333  uint32_t getUint32() const;
334 
340  void setUint8(uint8_t value);
341 
347  void setUint16(uint16_t value);
348 
354  void setUint32(uint32_t value);
355 
365  template<typename InputIterator>
366  void setData(InputIterator first, InputIterator last) {
367  data_.assign(first, last);
368  }
369 
374  void setEncapsulatedSpace(const std::string& encapsulated_space) {
375  encapsulated_space_ = encapsulated_space;
376  }
377 
381  std::string getEncapsulatedSpace() const {
382  return (encapsulated_space_);
383  }
384 
386  virtual ~Option();
387 
396  bool equals(const OptionPtr& other) const;
397 
410  virtual bool equals(const Option& other) const;
411 
412 protected:
413 
423  template<typename OptionType>
425  const OptionType* cast_this = dynamic_cast<const OptionType*>(this);
426  if (cast_this) {
427  return (boost::shared_ptr<OptionType>(new OptionType(*cast_this)));
428  }
429  return (OptionPtr());
430  }
431 
446  void packHeader(isc::util::OutputBuffer& buf) const;
447 
461  void packOptions(isc::util::OutputBuffer& buf) const;
462 
475  void unpackOptions(const OptionBuffer& buf);
476 
487  std::string headerToText(const int indent = 0,
488  const std::string& type_name = "") const;
489 
502  std::string suboptionsToText(const int indent = 0) const;
503 
509  void check() const;
510 
513 
515  uint16_t type_;
516 
519 
522 
524  std::string encapsulated_space_;
525 
528 };
529 
530 } // namespace isc::dhcp
531 } // namespace isc
532 
533 #endif // OPTION_H
void getOptionsCopy(OptionCollection &options_copy) const
Performs deep copy of suboptions.
Definition: option.cc:211
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition: option.cc:313
uint16_t getUint16() const
Returns content of first word.
Definition: option.cc:357
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:246
void packHeader(isc::util::OutputBuffer &buf) const
Store option's header in a buffer.
Definition: option.cc:117
Exception thrown during option unpacking This exception is thrown when an error has occurred,...
Definition: option.h:52
virtual ~Option()
just to force that every option has virtual dtor
Definition: option.cc:391
virtual std::vector< uint8_t > toBinary(const bool include_header=false) const
Returns binary representation of the option.
Definition: option.cc:259
boost::shared_ptr< OptionBuffer > OptionBufferPtr
pointer to a DHCP buffer
Definition: option.h:34
void setUint16(uint16_t value)
Sets content of this option to singe uint16 value.
Definition: option.cc:372
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:67
static OptionPtr factory(Option::Universe u, uint16_t type)
Factory function to create instance of option.
Definition: option.h:115
uint32_t getUint32() const
Returns content of first double word.
Definition: option.cc:362
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
Definition: option.cc:36
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition: option.h:424
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:328
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:512
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:25
static const size_t OPTION4_HDR_LEN
length of the usual DHCPv4 option header (there are exceptions)
Definition: option.h:61
void setUint32(uint32_t value)
Sets content of this option to singe uint32 value.
Definition: option.cc:377
const OptionCollection & getOptions() const
Returns all encapsulated options.
Definition: option.h:296
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:294
Universe getUniverse() const
returns option universe (V4 or V6)
Definition: option.h:190
virtual std::string toString() const
Returns string representation of the value.
Definition: option.cc:253
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option.cc:77
bool equals(const OptionPtr &other) const
Checks if options are equal.
Definition: option.cc:382
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
Definition: option.cc:224
virtual void pack(isc::util::OutputBuffer &buf) const
Writes option in wire-format to a buffer.
Definition: option.cc:105
OptionPtr getOption(uint16_t type) const
Returns shared_ptr to suboption of specific type.
Definition: option.cc:201
OptionCollection options_
collection for storing suboptions
Definition: option.h:521
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition: option.h:268
void check() const
A protected method used for option correctness.
Definition: option.cc:82
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition: option.h:374
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition: option.h:41
void addOption(OptionPtr opt)
Adds a sub-option.
Definition: option.cc:338
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:31
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.
void packOptions(isc::util::OutputBuffer &buf) const
Store sub options in a buffer.
Definition: option.cc:137
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header)
Definition: option.cc:171
virtual bool valid() const
returns if option is valid (e.g.
Definition: option.cc:192
Option & operator=(const Option &rhs)
Assignment operator.
Definition: option.cc:65
static OptionPtr factory(Option::Universe u, uint16_t type, const OptionBuffer &buf)
Factory function to create instance of option.
Definition: option.cc:29
void unpackOptions(const OptionBuffer &buf)
Builds a collection of sub options from the buffer.
Definition: option.cc:156
std::string getEncapsulatedSpace() const
Returns the name of the option space encapsulated by this option.
Definition: option.h:381
OptionBuffer::iterator OptionBufferIter
iterator for walking over OptionBuffer
Definition: option.h:28
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition: option.cc:150
void setUint8(uint8_t value)
Sets content of this option to singe uint8 value.
Definition: option.cc:367
OptionBuffer data_
contains content of this data
Definition: option.h:518
virtual std::string toHexString(const bool include_header=false) const
Returns string containing hexadecimal representation of option.
Definition: option.cc:280
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:515
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition: option.h:366
uint8_t getUint8() const
Returns content of first byte.
Definition: option.cc:349
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:524
virtual std::string toText(int indent=0) const
Returns string representation of the option.
Definition: option.cc:234
OptionPtr Factory(Option::Universe u, uint16_t type, const OptionBuffer &buf)
a factory function prototype
Definition: option.h:80
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:64