Kea 2.7.5
dhcp6/parser_context.cc
Go to the documentation of this file.
1// Copyright (C) 2016-2024 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#include <config.h>
8
10#include <dhcp6/dhcp6_parser.h>
11#include <dhcp6/dhcp6_log.h>
13#include <cc/data.h>
14#include <boost/lexical_cast.hpp>
15#include <fstream>
16#include <sstream>
17#include <limits>
18
19namespace isc {
20namespace dhcp {
21
23 : sfile_(nullptr), ctx_(NO_KEYWORD), trace_scanning_(false),
24 trace_parsing_(false) {
25}
26
29
31Parser6Context::parseString(const std::string& str, ParserType parser_type) {
32 scanStringBegin(str, parser_type);
33 return (parseCommon());
34}
35
37Parser6Context::parseFile(const std::string& filename, ParserType parser_type) {
38 FILE* f = fopen(filename.c_str(), "r");
39 if (!f) {
40 isc_throw(Dhcp6ParseError, "Unable to open file " << filename);
41 }
42 scanFileBegin(f, filename, parser_type);
43 return (parseCommon());
44}
45
47Parser6Context::parseCommon() {
48 isc::dhcp::Dhcp6Parser parser(*this);
49 // Uncomment this to get detailed parser logs.
50 // trace_parsing_ = true;
51 parser.set_debug_level(trace_parsing_);
52 try {
53 int res = parser.parse();
54 if (res != 0) {
55 isc_throw(Dhcp6ParseError, "Parser abort");
56 }
57 scanEnd();
58 } catch (...) {
59 scanEnd();
60 throw;
61 }
62 if (stack_.size() == 1) {
63 return (stack_[0]);
64 } else {
65 isc_throw(Dhcp6ParseError, "Expected exactly one terminal Element, found "
66 << stack_.size());
67 }
68}
69
70void
71Parser6Context::error(const isc::dhcp::location& loc,
72 const std::string& what,
73 size_t pos) {
74 if (pos == 0) {
75 isc_throw(Dhcp6ParseError, loc << ": " << what);
76 } else {
77 isc_throw(Dhcp6ParseError, loc << " (near " << pos << "): " << what);
78 }
79}
80
81void
82Parser6Context::error(const std::string& what) {
84}
85
86void
87Parser6Context::fatal(const std::string& what) {
89}
90
92Parser6Context::loc2pos(isc::dhcp::location& loc) {
93 const std::string& file = *loc.begin.filename;
94 const uint32_t line = loc.begin.line;
95 const uint32_t pos = loc.begin.column;
96 return (isc::data::Element::Position(file, line, pos));
97}
98
99void
100Parser6Context::require(const std::string& name,
103 ConstElementPtr value = stack_.back()->get(name);
104 if (!value) {
106 "missing parameter '" << name << "' ("
107 << stack_.back()->getPosition() << ") ["
108 << contextName() << " map between "
109 << open_loc << " and " << close_loc << "]");
110 }
111}
112
113void
114Parser6Context::unique(const std::string& name,
116 ConstElementPtr value = stack_.back()->get(name);
117 if (value) {
118 if (ctx_ != NO_KEYWORD) {
119 isc_throw(Dhcp6ParseError, loc << ": duplicate " << name
120 << " entries in " << contextName()
121 << " map (previous at " << value->getPosition() << ")");
122 } else {
123 isc_throw(Dhcp6ParseError, loc << ": duplicate " << name
124 << " entries in JSON"
125 << " map (previous at " << value->getPosition() << ")");
126 }
127 }
128}
129
130void
132 cstack_.push_back(ctx_);
133 ctx_ = ctx;
134}
135
136void
138#if 1
139 if (cstack_.empty()) {
140 fatal("unbalanced syntactic context");
141 }
142#endif
143 ctx_ = cstack_.back();
144 cstack_.pop_back();
145}
146
147const std::string
149 switch (ctx_) {
150 case NO_KEYWORD:
151 return ("__no keyword__");
152 case CONFIG:
153 return ("toplevel");
154 case DHCP6:
155 return ("Dhcp6");
157 return ("interfaces-config");
158 case LEASE_DATABASE:
159 return ("lease-database");
160 case HOSTS_DATABASE:
161 return ("hosts-database");
162 case DATABASE_ON_FAIL:
163 return ("database-on-fail");
164 case MAC_SOURCES:
165 return ("mac-sources");
167 return ("host-reservation-identifiers");
168 case HOOKS_LIBRARIES:
169 return ("hooks-libraries");
170 case SUBNET6:
171 return ("subnet6");
172 case OPTION_DEF:
173 return ("option-def");
174 case OPTION_DATA:
175 return ("option-data");
176 case CLIENT_CLASSES:
177 return ("client-classes");
179 return ("expired-leases-processing");
180 case SERVER_ID:
181 return ("server-id");
182 case DUID_TYPE:
183 return ("duid-type");
184 case CONTROL_SOCKET:
185 return ("control-socket");
187 return ("control-socket-type");
188 case AUTHENTICATION:
189 return ("authentication");
190 case AUTH_TYPE:
191 return ("auth-type");
192 case CLIENTS:
193 return ("clients");
195 return ("dhcp-queue-control");
197 return ("multi-threading");
198 case POOLS:
199 return ("pools");
200 case PD_POOLS:
201 return ("pd-pools");
202 case RESERVATIONS:
203 return ("reservations");
204 case RELAY:
205 return ("relay");
206 case LOGGERS:
207 return ("loggers");
208 case OUTPUT_OPTIONS:
209 return ("output-options");
210 case DHCP_DDNS:
211 return ("dhcp-ddns");
212 case NCR_PROTOCOL:
213 return ("ncr-protocol");
214 case NCR_FORMAT:
215 return ("ncr-format");
217 return ("replace-client-name");
218 case SHARED_NETWORK:
219 return ("shared-networks");
220 case SANITY_CHECKS:
221 return ("sanity-checks");
222 case CONFIG_CONTROL:
223 return ("config-control");
224 case CONFIG_DATABASE:
225 return ("config-database");
226 case COMPATIBILITY:
227 return ("compatibility");
229 return ("ddns-conflict-resolution-mode");
230 default:
231 return ("__unknown__");
232 }
233}
234
235void
236Parser6Context::warning(const isc::dhcp::location& loc,
237 const std::string& what) {
238 std::ostringstream msg;
239 msg << loc << ": " << what;
241 .arg(msg.str());
242}
243
244void
245Parser6Context::warnAboutExtraCommas(const isc::dhcp::location& loc) {
246 warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
247}
248
249} // namespace dhcp
250} // namespace isc
Evaluation error exception raised when trying to parse.
void require(const std::string &name, isc::data::Element::Position open_loc, isc::data::Element::Position close_loc)
Check if a required parameter is present.
static void fatal(const std::string &what)
Fatal error handler.
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
isc::data::Element::Position loc2pos(isc::dhcp::location &loc)
Converts bison's position to one understandable by isc::data::Element.
virtual ~Parser6Context()
destructor
void warning(const isc::dhcp::location &loc, const std::string &what)
Warning handler.
void leave()
Leave a syntactic context.
void unique(const std::string &name, isc::data::Element::Position loc)
Check if a parameter is already present.
void enter(const ParserContext &ctx)
Enter a new syntactic context.
void error(const isc::dhcp::location &loc, const std::string &what, size_t pos=0)
Error handler.
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
Parser6Context()
Default constructor.
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
ParserContext
Defines syntactic contexts for lexical tie-ins.
@ LOGGERS
Used while parsing Dhcp6/loggers structures.
@ CLIENT_CLASSES
Used while parsing Dhcp6/client-classes structures.
@ DATABASE_ON_FAIL
Used while parsing Dhcp6/*-database/on-fail.
@ OPTION_DEF
Used while parsing Dhcp6/option-def structures.
@ POOLS
Used while parsing Dhcp6/subnet6/pools structures.
@ AUTHENTICATION
Used while parsing Dhcp6/control-socket/authentication structures.
@ EXPIRED_LEASES_PROCESSING
Used while parsing Dhcp6/expired-leases-processing.
@ OPTION_DATA
Used while parsing Dhcp6/option-data, Dhcp6/subnet6/option-data or anywhere option-data is present (c...
@ CONTROL_SOCKET
Used while parsing Dhcp6/control-socket structures.
@ DHCP_QUEUE_CONTROL
Used while parsing Dhcp6/dhcp-queue-control structures.
@ SERVER_ID
Used while parsing Dhcp6/server-id structures.
@ HOSTS_DATABASE
Used while parsing Dhcp6/hosts-database[s] structures.
@ SUBNET6
Used while parsing Dhcp6/Subnet6 structures.
@ RESERVATIONS
Used while parsing Dhcp6/reservations structures.
@ CONFIG_DATABASE
Used while parsing config-control/config-databases.
@ AUTH_TYPE
Used while parsing Dhcp6/control-socket/authentication/type structures.
@ DHCP_DDNS
Used while parsing Dhcp6/dhcp-ddns.
@ COMPATIBILITY
Used while parsing compatibility parameters.
@ INTERFACES_CONFIG
Used while parsing Dhcp6/interfaces structures.
@ DUID_TYPE
Used while parsing Dhcp6/server-id/type structures.
@ HOOKS_LIBRARIES
Used while parsing Dhcp6/hooks-libraries.
@ CONFIG
Used while parsing content of Dhcp6.
@ DDNS_CONFLICT_RESOLUTION_MODE
Used while parsing Dhcp6/ddns-conflict-resolution-mode.
@ LEASE_DATABASE
Used while parsing Dhcp6/lease-database structures.
@ CLIENTS
Used while parsing Dhcp6/control-socket/authentication/clients structures.
@ NCR_PROTOCOL
Used while parsing Dhcp6/dhcp-ddns/ncr-protocol.
@ RELAY
Used while parsing Dhcp6/subnet6/relay structures.
@ OUTPUT_OPTIONS
Used while parsing Dhcp6/loggers/output-options structures.
@ HOST_RESERVATION_IDENTIFIERS
Used while parsing Dhcp6/host-reservation-identifiers.
@ NCR_FORMAT
Used while parsing Dhcp6/dhcp-ddns/ncr-format.
@ REPLACE_CLIENT_NAME
Used while parsing Dhcp6/dhcp-ddns/replace-client-name.
@ DHCP_MULTI_THREADING
Used while parsing Dhcp6/multi-threading structures.
@ NO_KEYWORD
This one is used in pure JSON mode.
@ SHARED_NETWORK
Used while parsing shared-networks structures.
@ CONTROL_SOCKET_TYPE
Used while parsing Dhcp6/control-socket/socket-type structures.
@ PD_POOLS
Used while parsing Dhcp6/subnet6/pd-pools structures.
@ MAC_SOURCES
Used while parsing Dhcp6/mac-sources structures.
@ CONFIG_CONTROL
Used while parsing Dhcp6/config-control.
void warnAboutExtraCommas(const isc::dhcp::location &loc)
Warning for extra commas.
void scanEnd()
Method called after the last tokens are scanned.
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
ParserType
Defines currently supported scopes.
const std::string contextName()
Get the syntactic context name.
ParserContext ctx_
Current syntactic context.
Define the isc::dhcp::parser class.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
@ fatal
Definition db_log.h:117
const isc::log::MessageID DHCP6_CONFIG_SYNTAX_WARNING
isc::log::Logger dhcp6_logger(DHCP6_APP_LOGGER_NAME)
Base logger for DHCPv6 server.
Definition dhcp6_log.h:88
Defines the logger used by the top-level component of kea-lfc.
Represents the position of the data element within a configuration string.
Definition data.h:94