Kea  2.3.1-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 
417  static size_t getIdentifierMaxLength(const IdentifierType& type);
418 
429  void setIdentifier(const uint8_t* identifier, const size_t len,
430  const IdentifierType& type);
431 
442  void setIdentifier(const std::string& identifier, const std::string& name);
443 
448  HWAddrPtr getHWAddress() const;
449 
454  DuidPtr getDuid() const;
455 
460  const std::vector<uint8_t>& getIdentifier() const;
461 
464  IdentifierType getIdentifierType() const;
465 
470  static IdentifierType getIdentifierType(const std::string& identifier_name);
471 
475  std::string getIdentifierAsText() const;
476 
478  static std::string getIdentifierName(const IdentifierType& type);
479 
486  static std::string getIdentifierAsText(const IdentifierType& type,
487  const uint8_t* value,
488  const size_t length);
489 
493  void setIPv4SubnetID(const SubnetID ipv4_subnet_id) {
494  ipv4_subnet_id_ = ipv4_subnet_id;
495  }
496 
500  void setIPv6SubnetID(const SubnetID ipv6_subnet_id) {
501  ipv6_subnet_id_ = ipv6_subnet_id;
502  }
503 
506  return (ipv4_subnet_id_);
507  }
508 
511  return (ipv6_subnet_id_);
512  }
513 
522  void setIPv4Reservation(const asiolink::IOAddress& address);
523 
527  void removeIPv4Reservation();
528 
533  return (ipv4_reservation_);
534  }
535 
539  void addReservation(const IPv6Resrv& reservation);
540 
547  IPv6ResrvRange getIPv6Reservations(const IPv6Resrv::Type& type) const;
548 
553  IPv6ResrvRange getIPv6Reservations() const;
554 
558  bool hasIPv6Reservation() const;
559 
566  bool hasReservation(const IPv6Resrv& reservation) const;
567 
571  void setHostname(const std::string& hostname) {
572  hostname_ = hostname;
573  }
574 
576  const std::string& getHostname() const {
577  return (hostname_);
578  }
579 
581  std::string getLowerHostname() const {
582  return (boost::algorithm::to_lower_copy(hostname_));
583  }
584 
588  void addClientClass4(const std::string& class_name);
589 
592  return (dhcp4_client_classes_);
593  }
594 
598  void addClientClass6(const std::string& class_name);
599 
602  return (dhcp6_client_classes_);
603  }
604 
611  void setNextServer(const asiolink::IOAddress& next_server);
612 
615  return (next_server_);
616  }
617 
623  void setServerHostname(const std::string& server_host_name);
624 
626  const std::string& getServerHostname() const {
627  return (server_host_name_);
628  }
629 
635  void setBootFileName(const std::string& boot_file_name);
636 
638  const std::string& getBootFileName() const {
639  return (boot_file_name_);
640  }
641 
648  return (cfg_option4_);
649  }
650 
654  return (cfg_option4_);
655  }
656 
663  return (cfg_option6_);
664  }
665 
669  return (cfg_option6_);
670  }
671 
673  std::string toText() const;
674 
677  void setHostId(HostID id) {
678  host_id_ = id;
679  }
680 
683  HostID getHostId() const {
684  return (host_id_);
685  }
686 
692  void setNegative(bool negative) {
693  negative_ = negative;
694  }
695 
698  bool getNegative() const {
699  return (negative_);
700  }
701 
705  isc::data::ElementPtr toElement4() const;
706 
710  isc::data::ElementPtr toElement6() const;
711 
715  void setKey(const AuthKey& key) {
716  key_ = key;
717  }
718 
722  AuthKey getKey() const {
723  return(key_);
724  }
725 
726 protected:
727 
733  void setIdentifierType(const IdentifierType& type);
734 
735 private:
736 
747  void addClientClassInternal(ClientClasses& classes,
748  const std::string& class_name);
749 
751  IdentifierType identifier_type_;
753  std::vector<uint8_t> identifier_value_;
755  SubnetID ipv4_subnet_id_;
757  SubnetID ipv6_subnet_id_;
759  asiolink::IOAddress ipv4_reservation_;
761  IPv6ResrvCollection ipv6_reservations_;
763  std::string hostname_;
765  ClientClasses dhcp4_client_classes_;
767  ClientClasses dhcp6_client_classes_;
769  asiolink::IOAddress next_server_;
771  std::string server_host_name_;
773  std::string boot_file_name_;
774 
777  uint64_t host_id_;
778 
780  CfgOptionPtr cfg_option4_;
782  CfgOptionPtr cfg_option6_;
783 
789  bool negative_;
790 
797  AuthKey key_;
798 };
799 
801 typedef boost::shared_ptr<Host> HostPtr;
802 
804 typedef boost::shared_ptr<const Host> ConstHostPtr;
805 
807 typedef std::vector<ConstHostPtr> ConstHostCollection;
808 
810 typedef std::vector<HostPtr> HostCollection;
811 
812 }
813 }
814 
815 #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:722
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:745
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:571
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:576
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:801
Base class for user context.
Definition: user_context.h:22
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition: cfg_option.h:748
void setKey(const AuthKey &key)
sets key.
Definition: host.h:715
Defines elements for storing the names of client classes.
bool getNegative() const
Return the negative cache flag value.
Definition: host.h:698
const std::string & getBootFileName() const
Returns value of boot file name (file).
Definition: host.h:638
HostID getHostId() const
Returns Host ID (primary key in MySQL and PostgreSQL backends)
Definition: host.h:683
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:810
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:662
const ClientClasses & getClientClasses6() const
Returns classes which DHCPv6 client is associated with.
Definition: host.h:601
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:614
void setHostId(HostID id)
Sets Host ID (primary key in MySQL and PostgreSQL backends)
Definition: host.h:677
std::pair< IPv6ResrvIterator, IPv6ResrvIterator > IPv6ResrvRange
Definition: host.h:243
std::vector< ConstHostPtr > ConstHostCollection
Collection of the const Host objects.
Definition: host.h:807
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:626
SubnetID getIPv4SubnetID() const
Returns subnet identifier for IPv4 reservation.
Definition: host.h:505
Flexible host identifier.
Definition: host.h:312
boost::shared_ptr< const Host > ConstHostPtr
Const pointer to the Host object.
Definition: host.h:804
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:532
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:653
bool operator==(const AuthKey &other) const
Equality operator.
Definition: host.cc:74
SubnetID getIPv6SubnetID() const
Returns subnet identifier for IPv6 reservations.
Definition: host.h:510
const ClientClasses & getClientClasses4() const
Returns classes which DHCPv4 client is associated with.
Definition: host.h:591
std::string getLowerHostname() const
Returns reserved hostname in lower case.
Definition: host.h:581
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:493
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:70
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:668
CfgOptionPtr getCfgOption4()
Returns pointer to the DHCPv4 option data configuration for this host.
Definition: host.h:647
void setIPv6SubnetID(const SubnetID ipv6_subnet_id)
Sets new IPv6 subnet identifier.
Definition: host.h:500
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:692