Kea: host_data_parser.h Source File

| Kea 3.0.2-git

host_data_parser.h
Go to the documentation of this file.
1// Copyright (C) 2017-2025 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_DATA_PARSER_H
8#define HOST_DATA_PARSER_H
9
10#include <dhcpsrv/cfg_host_operations.h>
11#include <dhcpsrv/parsers/host_reservation_parser.h>
12#include <boost/pointer_cast.hpp>
13#include <boost/shared_ptr.hpp>
14
15namespace isc {
16namespace host_cmds {
17
29template<typename ParserBase>
30 class HostDataParser : public ParserBase {
31public:
32
48 isc::dhcp::HostPtr parseWithSubnet(isc::data::ConstElementPtr host_data,
49 bool required = true) {
50 uint32_t subnet_id = dhcp::SUBNET_ID_UNUSED;
51 boost::shared_ptr<isc::data::MapElement> new_map = boost::dynamic_pointer_cast<
52 isc::data::MapElement>(isc::data::Element::createMap());
53
54 for (auto const& el : host_data->mapValue()) {
55 if (el.first == "subnet-id") {
56 if (el.second->getType() != isc::data::Element::integer) {
57 isc_throw(isc::BadValue,
58 "'subnet-id' parameter is not integer.");
59 }
60
61 if (el.second->intValue() < 0 ||
62 el.second->intValue() > dhcp::SUBNET_ID_MAX) {
63 isc_throw(isc::BadValue,
64 "subnet-id: " << el.second->intValue()
65 << " is invalid," << " must be between 0 and "
66 << dhcp::SUBNET_ID_MAX << " (inclusive).");
67 }
68
69 subnet_id = static_cast<uint32_t>(el.second->intValue());
70 } else {
71 new_map->set(el.first, el.second);
72 }
73 }
74
75 if (required && (subnet_id == dhcp::SUBNET_ID_UNUSED)) {
76 isc_throw(isc::BadValue,
77 "Mandatory 'subnet-id' parameter missing."
78 << " Use `reservation-get-by-id' or"
79 << " 'reservation-get-by-address' to get the list of"
80 << " reservations with given identifier or address.");
81 }
82
83 // Parse host reservations without encapsulating options with suboptions.
84 return (ParserBase::parse(subnet_id,
85 boost::dynamic_pointer_cast<const isc::data::Element>(new_map),
86 false));
87 }
88};
89
91 typedef HostDataParser<isc::dhcp::HostReservationParser4> HostDataParser4;
92
94 typedef HostDataParser<isc::dhcp::HostReservationParser6> HostDataParser6;
95
96} // namespace host_cmds
97} // namespace isc
98
99#endif
cfg_host_operations.h
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::data::Element::integer
@ integer
Definition data.h:140
isc::data::Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
isc::data::MapElement
Definition data.h:768
isc::host_cmds::HostDataParser
A class for parsing extended host reservation data.
Definition host_data_parser.h:30
isc::host_cmds::HostDataParser::parseWithSubnet
isc::dhcp::HostPtr parseWithSubnet(isc::data::ConstElementPtr host_data, bool required=true)
Parser specified parameter as host reservation data.
Definition host_data_parser.h:48
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
host_reservation_parser.h
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::dhcp::HostPtr
boost::shared_ptr< Host > HostPtr
Pointer to the Host object.
Definition host.h:837
isc::host_cmds
Definition host_cmds.cc:39
isc::host_cmds::HostDataParser6
HostDataParser< isc::dhcp::HostReservationParser6 > HostDataParser6
Parser for DHCPv4 host reservation.
Definition host_data_parser.h:94
isc::host_cmds::HostDataParser4
HostDataParser< isc::dhcp::HostReservationParser4 > HostDataParser4
Parser for DHCPv4 host reservation.
Definition host_data_parser.h:91
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148