Kea  2.1.7-git
host.h
Go to the documentation of this file.
1 // Copyright (C) 2014-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 HOST_H
8 #define HOST_H
9 
10 #include <asiolink/io_address.h>
11 #include <cc/data.h>
12 #include <cc/user_context.h>
13 #include <dhcp/classify.h>
14 #include <dhcp/duid.h>
15 #include <dhcp/hwaddr.h>
16 #include <dhcpsrv/cfg_option.h>
17 #include <dhcpsrv/subnet_id.h>
18 #include <boost/shared_ptr.hpp>
19 #include <boost/algorithm/string.hpp>
20 #include <list>
21 #include <map>
22 #include <string>
23 #include <utility>
24 
25 namespace isc {
26 namespace dhcp {
27 
32 const size_t ADDRESS6_TEXT_MAX_LEN = 39;
33 
36 const size_t CLIENT_CLASSES_MAX_LEN = 255;
37 
42 const size_t HOSTNAME_MAX_LEN = 255;
43 
45 const size_t OPTION_VALUE_MAX_LEN = 4096;
46 
48 const size_t OPTION_FORMATTED_VALUE_MAX_LEN = 8192;
49 
51 const size_t OPTION_SPACE_MAX_LEN = 128;
52 
54 const size_t USER_CONTEXT_MAX_LEN = 8192;
55 
57 const size_t SERVER_HOSTNAME_MAX_LEN = 64;
58 
60 const size_t BOOT_FILE_NAME_MAX_LEN = 128;
61 
63 const uint8_t AUTH_KEY_LEN = 16;
64 
66 const size_t TEXT_AUTH_KEY_LEN = AUTH_KEY_LEN * 2;
67 
69 typedef uint64_t HostID;
70 
75 class AuthKey {
76 public:
82  AuthKey(const std::vector<uint8_t>& key);
83 
92  AuthKey(const std::string& key);
93 
98  AuthKey();
99 
100  // @brief Get random string.
105  static std::vector<uint8_t> getRandomKeyString();
106 
110  // If the size is greater than 16 bytes, we resize to 16 bytes.
113  void setAuthKey(const std::vector<uint8_t>& key);
114 
122  void setAuthKey(const std::string& key);
123 
127  const std::vector<uint8_t>& getAuthKey() const {
128  return authKey_;
129  }
130 
134  std::string toText() const;
135 
141  bool operator==(const AuthKey& other) const;
142 
147  bool operator!=(const AuthKey& other) const;
148 
149 private:
150  std::vector<uint8_t> authKey_;
151 };
152 
161 class IPv6Resrv {
162 public:
163 
167  enum Type {
169  TYPE_PD
170  };
171 
185  IPv6Resrv(const Type& type,
186  const asiolink::IOAddress& prefix,
187  const uint8_t prefix_len = 128);
188 
191  return (prefix_);
192  }
193 
195  uint8_t getPrefixLen() const {
196  return (prefix_len_);
197  }
198 
204  Type getType() const {
205  return (type_);
206  }
207 
216  void set(const Type& type, const asiolink::IOAddress& prefix,
217  const uint8_t prefix_len);
218 
220  std::string toText() const;
221 
225  bool operator==(const IPv6Resrv& other) const;
226 
230  bool operator!=(const IPv6Resrv& other) const;
231 
232 private:
233 
234  Type type_;
235  asiolink::IOAddress prefix_;
236  uint8_t prefix_len_;
237 };
238 
240 typedef std::multimap<IPv6Resrv::Type, IPv6Resrv> IPv6ResrvCollection;
241 typedef IPv6ResrvCollection::const_iterator IPv6ResrvIterator;
242 typedef std::pair<IPv6Resrv::Type, IPv6Resrv> IPv6ResrvTuple;
243 typedef std::pair<IPv6ResrvIterator, IPv6ResrvIterator> IPv6ResrvRange;
244 
297 class Host : public data::UserContext {
298 public:
299 
313  };
314 
317  static const IdentifierType LAST_IDENTIFIER_TYPE = IDENT_FLEX;
318 
349  Host(const uint8_t* identifier, const size_t identifier_len,
350  const IdentifierType& identifier_type,
351  const SubnetID ipv4_subnet_id, const SubnetID ipv6_subnet_id,
352  const asiolink::IOAddress& ipv4_reservation,
353  const std::string& hostname = "",
354  const std::string& dhcp4_client_classes = "",
355  const std::string& dhcp6_client_classes = "",
357  const std::string& server_host_name = "",
358  const std::string& boot_file_name = "",
359  const AuthKey& auth_key = AuthKey(""));
360 
401  Host(const std::string& identifier, const std::string& identifier_name,
402  const SubnetID ipv4_subnet_id, const SubnetID ipv6_subnet_id,
403  const asiolink::IOAddress& ipv4_reservation,
404  const std::string& hostname = "",
405  const std::string& dhcp4_client_classes = "",
406  const std::string& dhcp6_client_classes = "",
408  const std::string& server_host_name = "",
409  const std::string& boot_file_name = "",
410  const AuthKey& auth_key = AuthKey(""));
411 
422  void setIdentifier(const uint8_t* identifier, const size_t len,
423  const IdentifierType& type);
424 
435  void setIdentifier(const std::string& identifier, const std::string& name);
436 
441  HWAddrPtr getHWAddress() const;
442 
447  DuidPtr getDuid() const;
448 
453  const std::vector<uint8_t>& getIdentifier() const;
454 
457  IdentifierType getIdentifierType() const;
458 
463  static IdentifierType getIdentifierType(const std::string& identifier_name);
464 
468  std::string getIdentifierAsText() const;
469 
471  static std::string getIdentifierName(const IdentifierType& type);
472 
479  static std::string getIdentifierAsText(const IdentifierType& type,
480  const uint8_t* value,
481  const size_t length);
482 
486  void setIPv4SubnetID(const SubnetID ipv4_subnet_id) {
487  ipv4_subnet_id_ = ipv4_subnet_id;
488  }
489 
493  void setIPv6SubnetID(const SubnetID ipv6_subnet_id) {
494  ipv6_subnet_id_ = ipv6_subnet_id;
495  }
496 
499  return (ipv4_subnet_id_);
500  }
501 
504  return (ipv6_subnet_id_);
505  }
506 
515  void setIPv4Reservation(const asiolink::IOAddress& address);
516 
520  void removeIPv4Reservation();
521 
526  return (ipv4_reservation_);
527  }
528 
532  void addReservation(const IPv6Resrv& reservation);
533 
540  IPv6ResrvRange getIPv6Reservations(const IPv6Resrv::Type& type) const;
541 
546  IPv6ResrvRange getIPv6Reservations() const;
547 
551  bool hasIPv6Reservation() const;
552 
559  bool hasReservation(const IPv6Resrv& reservation) const;
560 
564  void setHostname(const std::string& hostname) {
565  hostname_ = hostname;
566  }
567 
569  const std::string& getHostname() const {
570  return (hostname_);
571  }
572 
574  std::string getLowerHostname() const {
575  return (boost::algorithm::to_lower_copy(hostname_));
576  }
577 
581  void addClientClass4(const std::string& class_name);
582 
585  return (dhcp4_client_classes_);
586  }
587 
591  void addClientClass6(const std::string& class_name);
592 
595  return (dhcp6_client_classes_);
596  }
597 
604  void setNextServer(const asiolink::IOAddress& next_server);
605 
608  return (next_server_);
609  }
610 
616  void setServerHostname(const std::string& server_host_name);
617 
619  const std::string& getServerHostname() const {
620  return (server_host_name_);
621  }
622 
628  void setBootFileName(const std::string& boot_file_name);
629 
631  const std::string& getBootFileName() const {
632  return (boot_file_name_);
633  }
634 
641  return (cfg_option4_);
642  }
643 
647  return (cfg_option4_);
648  }
649 
656  return (cfg_option6_);
657  }
658 
662  return (cfg_option6_);
663  }
664 
666  std::string toText() const;
667 
670  void setHostId(HostID id) {
671  host_id_ = id;
672  }
673 
676  HostID getHostId() const {
677  return (host_id_);
678  }
679 
685  void setNegative(bool negative) {
686  negative_ = negative;
687  }
688 
691  bool getNegative() const {
692  return (negative_);
693  }
694 
698  isc::data::ElementPtr toElement4() const;
699 
703  isc::data::ElementPtr toElement6() const;
704 
708  void setKey(const AuthKey& key) {
709  key_ = key;
710  }
711 
715  AuthKey getKey() const {
716  return(key_);
717  }
718 
719 private:
720 
731  void addClientClassInternal(ClientClasses& classes,
732  const std::string& class_name);
733 
735  IdentifierType identifier_type_;
737  std::vector<uint8_t> identifier_value_;
739  SubnetID ipv4_subnet_id_;
741  SubnetID ipv6_subnet_id_;
743  asiolink::IOAddress ipv4_reservation_;
745  IPv6ResrvCollection ipv6_reservations_;
747  std::string hostname_;
749  ClientClasses dhcp4_client_classes_;
751  ClientClasses dhcp6_client_classes_;
753  asiolink::IOAddress next_server_;
755  std::string server_host_name_;
757  std::string boot_file_name_;
758 
761  uint64_t host_id_;
762 
764  CfgOptionPtr cfg_option4_;
766  CfgOptionPtr cfg_option6_;
767 
773  bool negative_;
774 
781  AuthKey key_;
782 };
783 
785 typedef boost::shared_ptr<Host> HostPtr;
786 
788 typedef boost::shared_ptr<const Host> ConstHostPtr;
789 
791 typedef std::vector<ConstHostPtr> ConstHostCollection;
792 
794 typedef std::vector<HostPtr> HostCollection;
795 
796 }
797 }
798 
799 #endif // HOST_H
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:20
const std::vector< uint8_t > & getAuthKey() const
Return auth key.
Definition: host.h:127
const size_t ADDRESS6_TEXT_MAX_LEN
Maximum size of an IPv6 address represented as a text string.
Definition: host.h:32
AuthKey()
Constructor.
Definition: host.cc:33
AuthKey getKey() const
Returns the key.
Definition: host.h:715
const size_t OPTION_FORMATTED_VALUE_MAX_LEN
Maximum length of option value specified in textual format.
Definition: host.h:48
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition: cfg_option.h:706
const size_t USER_CONTEXT_MAX_LEN
Maximum length of user context.
Definition: host.h:54
const size_t CLIENT_CLASSES_MAX_LEN
Maximum length of classes stored in a dhcp4/6_client_classes columns.
Definition: host.h:36
void setHostname(const std::string &hostname)
Sets new hostname.
Definition: host.h:564
uint8_t getPrefixLen() const
Returns prefix length.
Definition: host.h:195
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
const std::string & getHostname() const
Returns reserved hostname.
Definition: host.h:569
const size_t BOOT_FILE_NAME_MAX_LEN
Maximum length of the boot file name.
Definition: host.h:60
boost::shared_ptr< Host > HostPtr
Pointer to the Host object.
Definition: host.h:785
Base class for user context.
Definition: user_context.h:22
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition: cfg_option.h:709
void setKey(const AuthKey &key)
sets key.
Definition: host.h:708
Defines elements for storing the names of client classes.
bool getNegative() const
Return the negative cache flag value.
Definition: host.h:691
const std::string & getBootFileName() const
Returns value of boot file name (file).
Definition: host.h:631
HostID getHostId() const
Returns Host ID (primary key in MySQL and PostgreSQL backends)
Definition: host.h:676
static std::vector< uint8_t > getRandomKeyString()
Random string is generated by default will be used for the keys to be used for signing Reconfigure Me...
Definition: host.cc:38
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
std::vector< HostPtr > HostCollection
Collection of the Host objects.
Definition: host.h:794
IPv6 reservation for a host.
Definition: host.h:161
const size_t OPTION_SPACE_MAX_LEN
Maximum length of option space name.
Definition: host.h:51
CfgOptionPtr getCfgOption6()
Returns pointer to the DHCPv6 option data configuration for this host.
Definition: host.h:655
const ClientClasses & getClientClasses6() const
Returns classes which DHCPv6 client is associated with.
Definition: host.h:594
const size_t SERVER_HOSTNAME_MAX_LEN
Maximum length of the server hostname.
Definition: host.h:57
const size_t HOSTNAME_MAX_LEN
Maximum length of the hostname stored in DNS.
Definition: host.h:42
const asiolink::IOAddress & getNextServer() const
Returns value of next server field (siaddr).
Definition: host.h:607
void setHostId(HostID id)
Sets Host ID (primary key in MySQL and PostgreSQL backends)
Definition: host.h:670
std::pair< IPv6ResrvIterator, IPv6ResrvIterator > IPv6ResrvRange
Definition: host.h:243
std::vector< ConstHostPtr > ConstHostCollection
Collection of the const Host objects.
Definition: host.h:791
std::multimap< IPv6Resrv::Type, IPv6Resrv > IPv6ResrvCollection
Collection of IPv6 reservations for the host.
Definition: host.h:240
const std::string & getServerHostname() const
Returns value of server hostname (sname).
Definition: host.h:619
SubnetID getIPv4SubnetID() const
Returns subnet identifier for IPv4 reservation.
Definition: host.h:498
Flexible host identifier.
Definition: host.h:312
boost::shared_ptr< const Host > ConstHostPtr
Const pointer to the Host object.
Definition: host.h:788
bool operator!=(const AuthKey &other) const
Inequality operator.
Definition: host.cc:79
void setAuthKey(const std::vector< uint8_t > &key)
Set auth key value from binary.
Definition: host.cc:51
Represents a device with IPv4 and/or IPv6 reservations.
Definition: host.h:297
Type
Type of the reservation.
Definition: host.h:167
IPv6ResrvCollection::const_iterator IPv6ResrvIterator
Definition: host.h:241
const asiolink::IOAddress & getIPv4Reservation() const
Returns reserved IPv4 address.
Definition: host.h:525
Authentication keys.
Definition: host.h:75
Defines the logger used by the top-level component of kea-lfc.
Type getType() const
Returns reservation type.
Definition: host.h:204
ConstCfgOptionPtr getCfgOption4() const
Returns const pointer to the DHCPv4 option data configuration for this host.
Definition: host.h:646
bool operator==(const AuthKey &other) const
Equality operator.
Definition: host.cc:74
SubnetID getIPv6SubnetID() const
Returns subnet identifier for IPv6 reservations.
Definition: host.h:503
const ClientClasses & getClientClasses4() const
Returns classes which DHCPv4 client is associated with.
Definition: host.h:584
std::string getLowerHostname() const
Returns reserved hostname in lower case.
Definition: host.h:574
IdentifierType
Type of the host identifier.
Definition: host.h:307
const size_t TEXT_AUTH_KEY_LEN
Maximum length of authentication keys (coded in hexadecimal).
Definition: host.h:66
std::pair< IPv6Resrv::Type, IPv6Resrv > IPv6ResrvTuple
Definition: host.h:242
const uint8_t AUTH_KEY_LEN
Maximum length of authentication keys - 128 bits.
Definition: host.h:63
void setIPv4SubnetID(const SubnetID ipv4_subnet_id)
Sets new IPv4 subnet identifier.
Definition: host.h:486
const size_t OPTION_VALUE_MAX_LEN
Maximum length of option value.
Definition: host.h:45
std::string toText() const
Return text format for keys.
Definition: host.cc:43
uint64_t HostID
HostID (used only when storing in MySQL or PostgreSQL backends)
Definition: host.h:69
Container for storing client class names.
Definition: classify.h:68
const asiolink::IOAddress & getPrefix() const
Returns prefix for the reservation.
Definition: host.h:190
ConstCfgOptionPtr getCfgOption6() const
Returns const pointer to the DHCPv6 option data configuration for this host.
Definition: host.h:661
CfgOptionPtr getCfgOption4()
Returns pointer to the DHCPv4 option data configuration for this host.
Definition: host.h:640
void setIPv6SubnetID(const SubnetID ipv6_subnet_id)
Sets new IPv6 subnet identifier.
Definition: host.h:493
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition: subnet_id.h:24
void setNegative(bool negative)
Sets the negative cached flag.
Definition: host.h:685