Kea 2.5.8
dhcp6/parser_context.cc
Go to the documentation of this file.
1// Copyright (C) 2016-2023 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
28}
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 }
59 catch (...) {
60 scanEnd();
61 throw;
62 }
63 if (stack_.size() == 1) {
64 return (stack_[0]);
65 } else {
66 isc_throw(Dhcp6ParseError, "Expected exactly one terminal Element expected, found "
67 << stack_.size());
68 }
69}
70
71void
72Parser6Context::error(const isc::dhcp::location& loc,
73 const std::string& what,
74 size_t pos) {
75 if (pos == 0) {
76 isc_throw(Dhcp6ParseError, loc << ": " << what);
77 } else {
78 isc_throw(Dhcp6ParseError, loc << " (near " << pos << "): " << what);
79 }
80}
81
82void
83Parser6Context::error(const std::string& what) {
85}
86
87void
88Parser6Context::fatal(const std::string& what) {
90}
91
93Parser6Context::loc2pos(isc::dhcp::location& loc) {
94 const std::string& file = *loc.begin.filename;
95 const uint32_t line = loc.begin.line;
96 const uint32_t pos = loc.begin.column;
97 return (isc::data::Element::Position(file, line, pos));
98}
99
100void
101Parser6Context::require(const std::string& name,
104 ConstElementPtr value = stack_.back()->get(name);
105 if (!value) {
107 "missing parameter '" << name << "' ("
108 << stack_.back()->getPosition() << ") ["
109 << contextName() << " map between "
110 << open_loc << " and " << close_loc << "]");
111 }
112}
113
114void
115Parser6Context::unique(const std::string& name,
117 ConstElementPtr value = stack_.back()->get(name);
118 if (value) {
119 if (ctx_ != NO_KEYWORD) {
120 isc_throw(Dhcp6ParseError, loc << ": duplicate " << name
121 << " entries in " << contextName()
122 << " map (previous at " << value->getPosition() << ")");
123 } else {
124 isc_throw(Dhcp6ParseError, loc << ": duplicate " << name
125 << " entries in JSON"
126 << " map (previous at " << value->getPosition() << ")");
127 }
128 }
129}
130
131void
133 cstack_.push_back(ctx_);
134 ctx_ = ctx;
135}
136
137void
139#if 1
140 if (cstack_.empty()) {
141 fatal("unbalanced syntactic context");
142 }
143#endif
144 ctx_ = cstack_.back();
145 cstack_.pop_back();
146}
147
148const std::string
150 switch (ctx_) {
151 case NO_KEYWORD:
152 return ("__no keyword__");
153 case CONFIG:
154 return ("toplevel");
155 case DHCP6:
156 return ("Dhcp6");
158 return ("interfaces-config");
159 case LEASE_DATABASE:
160 return ("lease-database");
161 case HOSTS_DATABASE:
162 return ("hosts-database");
163 case DATABASE_TYPE:
164 return ("database-type");
165 case DATABASE_ON_FAIL:
166 return ("database-on-fail");
167 case MAC_SOURCES:
168 return ("mac-sources");
170 return ("host-reservation-identifiers");
171 case HOOKS_LIBRARIES:
172 return ("hooks-libraries");
173 case SUBNET6:
174 return ("subnet6");
175 case RESERVATION_MODE:
176 return ("reservation-mode");
177 case OPTION_DEF:
178 return ("option-def");
179 case OPTION_DATA:
180 return ("option-data");
181 case CLIENT_CLASSES:
182 return ("client-classes");
184 return ("expired-leases-processing");
185 case SERVER_ID:
186 return ("server-id");
187 case DUID_TYPE:
188 return ("duid-type");
189 case CONTROL_SOCKET:
190 return ("control-socket");
192 return ("dhcp-queue-control");
194 return ("multi-threading");
195 case POOLS:
196 return ("pools");
197 case PD_POOLS:
198 return ("pd-pools");
199 case RESERVATIONS:
200 return ("reservations");
201 case RELAY:
202 return ("relay");
203 case LOGGERS:
204 return ("loggers");
205 case OUTPUT_OPTIONS:
206 return ("output-options");
207 case DHCP_DDNS:
208 return ("dhcp-ddns");
209 case NCR_PROTOCOL:
210 return ("ncr-protocol");
211 case NCR_FORMAT:
212 return ("ncr-format");
214 return ("replace-client-name");
215 case SHARED_NETWORK:
216 return ("shared-networks");
217 case SANITY_CHECKS:
218 return ("sanity-checks");
219 case CONFIG_CONTROL:
220 return ("config-control");
221 case CONFIG_DATABASE:
222 return ("config-database");
223 case COMPATIBILITY:
224 return ("compatibility");
226 return ("ddns-conflict-resolution-mode");
227 default:
228 return ("__unknown__");
229 }
230}
231
232void
233Parser6Context::warning(const isc::dhcp::location& loc,
234 const std::string& what) {
235 std::ostringstream msg;
236 msg << loc << ": " << what;
238 .arg(msg.str());
239}
240
241void
242Parser6Context::warnAboutExtraCommas(const isc::dhcp::location& loc) {
243 warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
244}
245
246} // namespace dhcp
247} // namespace isc
Evaluation error exception raised when trying to parse.
A Bison parser.
Definition: dhcp6_parser.h:216
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.
@ RESERVATION_MODE
Used while parsing Dhcp6/reservation-mode.
@ 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.
@ 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.
@ DATABASE_TYPE
Used while parsing Dhcp6/*-database/type.
@ 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.
@ 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.
@ 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