Kea: parser_context.cc Source File

| Kea 3.2.0-git

dhcp6/parser_context.cc
Go to the documentation of this file.
1// Copyright (C) 2016-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#include <config.h>
8
9#include <dhcp6/parser_context.h>
10#include <dhcp6/dhcp6_parser.h>
11#include <dhcp6/dhcp6_log.h>
12#include <exceptions/exceptions.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
22Parser6Context::Parser6Context()
23 : sfile_(nullptr), ctx_(NO_KEYWORD), trace_scanning_(false),
24 trace_parsing_(false) {
25}
26
27Parser6Context::~Parser6Context() {
28}
29
30 isc::data::ElementPtr
31Parser6Context::parseString(const std::string& str, ParserType parser_type) {
32 scanStringBegin(str, parser_type);
33 return (parseCommon());
34}
35
36 isc::data::ElementPtr
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
46 isc::data::ElementPtr
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
82 Parser6Context::error(const std::string& what) {
83 isc_throw(Dhcp6ParseError, what);
84}
85
86void
87Parser6Context::fatal(const std::string& what) {
88 isc_throw(Dhcp6ParseError, what);
89}
90
91 isc::data::Element::Position
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,
101 isc::data::Element::Position open_loc,
102 isc::data::Element::Position close_loc) {
103 ConstElementPtr value = stack_.back()->get(name);
104 if (!value) {
105 isc_throw(Dhcp6ParseError,
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,
115 isc::data::Element::Position loc) {
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
131Parser6Context::enter(const ParserContext& ctx) {
132 cstack_.push_back(ctx_);
133 ctx_ = ctx;
134}
135
136void
137Parser6Context::leave() {
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
148Parser6Context::contextName() {
149 switch (ctx_) {
150 case NO_KEYWORD:
151 return ("__no keyword__");
152 case CONFIG:
153 return ("toplevel");
154 case DHCP6:
155 return ("Dhcp6");
156 case INTERFACES_CONFIG:
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 SSL_MODE:
165 return ("ssl-mode");
166 case MAC_SOURCES:
167 return ("mac-sources");
168 case HOST_RESERVATION_IDENTIFIERS:
169 return ("host-reservation-identifiers");
170 case HOOKS_LIBRARIES:
171 return ("hooks-libraries");
172 case SUBNET6:
173 return ("subnet6");
174 case OPTION_DEF:
175 return ("option-def");
176 case OPTION_DATA:
177 return ("option-data");
178 case CLIENT_CLASSES:
179 return ("client-classes");
180 case EXPIRED_LEASES_PROCESSING:
181 return ("expired-leases-processing");
182 case SERVER_ID:
183 return ("server-id");
184 case DUID_TYPE:
185 return ("duid-type");
186 case CONTROL_SOCKET:
187 return ("control-socket");
188 case CONTROL_SOCKET_TYPE:
189 return ("control-socket-type");
190 case AUTHENTICATION:
191 return ("authentication");
192 case AUTH_TYPE:
193 return ("auth-type");
194 case CLIENTS:
195 return ("clients");
196 case HTTP_HEADERS:
197 return ("http-headers");
198 case DHCP_QUEUE_CONTROL:
199 return ("dhcp-queue-control");
200 case DHCP_MULTI_THREADING:
201 return ("multi-threading");
202 case POOLS:
203 return ("pools");
204 case PD_POOLS:
205 return ("pd-pools");
206 case RESERVATIONS:
207 return ("reservations");
208 case RELAY:
209 return ("relay");
210 case LOGGERS:
211 return ("loggers");
212 case OUTPUT_OPTIONS:
213 return ("output-options");
214 case DHCP_DDNS:
215 return ("dhcp-ddns");
216 case NCR_PROTOCOL:
217 return ("ncr-protocol");
218 case NCR_FORMAT:
219 return ("ncr-format");
220 case REPLACE_CLIENT_NAME:
221 return ("replace-client-name");
222 case SHARED_NETWORK:
223 return ("shared-networks");
224 case SANITY_CHECKS:
225 return ("sanity-checks");
226 case CONFIG_CONTROL:
227 return ("config-control");
228 case CONFIG_DATABASE:
229 return ("config-database");
230 case COMPATIBILITY:
231 return ("compatibility");
232 case DDNS_CONFLICT_RESOLUTION_MODE:
233 return ("ddns-conflict-resolution-mode");
234 default:
235 return ("__unknown__");
236 }
237}
238
239void
240Parser6Context::warning(const isc::dhcp::location& loc,
241 const std::string& what) {
242 std::ostringstream msg;
243 msg << loc << ": " << what;
244 LOG_WARN(dhcp6_logger, DHCP6_CONFIG_SYNTAX_WARNING)
245 .arg(msg.str());
246}
247
248void
249Parser6Context::warnAboutExtraCommas(const isc::dhcp::location& loc) {
250 warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
251}
252
253} // namespace dhcp
254} // namespace isc
isc::dhcp::Dhcp6ParseError
Evaluation error exception raised when trying to parse.
Definition dhcp6/parser_context.h:28
isc::dhcp::Dhcp6Parser
A Bison parser.
Definition dhcp6_parser.h:216
isc::dhcp::Parser6Context::require
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.
Definition dhcp6/parser_context.cc:100
isc::dhcp::Parser6Context::fatal
static void fatal(const std::string &what)
Fatal error handler.
Definition dhcp6/parser_context.cc:87
isc::dhcp::Parser6Context::parseFile
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
Definition dhcp6/parser_context.cc:37
isc::dhcp::Parser6Context::loc2pos
isc::data::Element::Position loc2pos(isc::dhcp::location &loc)
Converts bison's position to one understandable by isc::data::Element.
Definition dhcp6/parser_context.cc:92
isc::dhcp::Parser6Context::~Parser6Context
virtual ~Parser6Context()
destructor
Definition dhcp6/parser_context.cc:27
isc::dhcp::Parser6Context::sfile_
FILE * sfile_
sFile (aka FILE)
Definition dhcp6/parser_context.h:384
isc::dhcp::Parser6Context::warning
void warning(const isc::dhcp::location &loc, const std::string &what)
Warning handler.
Definition dhcp6/parser_context.cc:240
isc::dhcp::Parser6Context::leave
void leave()
Leave a syntactic context.
Definition dhcp6/parser_context.cc:137
isc::dhcp::Parser6Context::unique
void unique(const std::string &name, isc::data::Element::Position loc)
Check if a parameter is already present.
Definition dhcp6/parser_context.cc:114
isc::dhcp::Parser6Context::enter
void enter(const ParserContext &ctx)
Enter a new syntactic context.
Definition dhcp6/parser_context.cc:131
isc::dhcp::Parser6Context::error
void error(const isc::dhcp::location &loc, const std::string &what, size_t pos=0)
Error handler.
Definition dhcp6/parser_context.cc:71
isc::dhcp::Parser6Context::stack_
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
Definition dhcp6/parser_context.h:104
isc::dhcp::Parser6Context::Parser6Context
Parser6Context()
Default constructor.
Definition dhcp6/parser_context.cc:22
isc::dhcp::Parser6Context::scanStringBegin
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
Definition dhcp6_lexer.cc:7248
isc::dhcp::Parser6Context::ParserContext
ParserContext
Defines syntactic contexts for lexical tie-ins.
Definition dhcp6/parser_context.h:233
isc::dhcp::Parser6Context::LOGGERS
@ LOGGERS
Used while parsing Dhcp6/loggers structures.
Definition dhcp6/parser_context.h:335
isc::dhcp::Parser6Context::DHCP6
@ DHCP6
Definition dhcp6/parser_context.h:241
isc::dhcp::Parser6Context::CLIENT_CLASSES
@ CLIENT_CLASSES
Used while parsing Dhcp6/client-classes structures.
Definition dhcp6/parser_context.h:285
isc::dhcp::Parser6Context::DATABASE_ON_FAIL
@ DATABASE_ON_FAIL
Used while parsing Dhcp6/*-database/on-fail.
Definition dhcp6/parser_context.h:256
isc::dhcp::Parser6Context::OPTION_DEF
@ OPTION_DEF
Used while parsing Dhcp6/option-def structures.
Definition dhcp6/parser_context.h:277
isc::dhcp::Parser6Context::POOLS
@ POOLS
Used while parsing Dhcp6/subnet6/pools structures.
Definition dhcp6/parser_context.h:323
isc::dhcp::Parser6Context::AUTHENTICATION
@ AUTHENTICATION
Used while parsing Dhcp6/control-socket/authentication structures.
Definition dhcp6/parser_context.h:303
isc::dhcp::Parser6Context::EXPIRED_LEASES_PROCESSING
@ EXPIRED_LEASES_PROCESSING
Used while parsing Dhcp6/expired-leases-processing.
Definition dhcp6/parser_context.h:288
isc::dhcp::Parser6Context::OPTION_DATA
@ OPTION_DATA
Used while parsing Dhcp6/option-data, Dhcp6/subnet6/option-data or anywhere option-data is present (c...
Definition dhcp6/parser_context.h:282
isc::dhcp::Parser6Context::CONTROL_SOCKET
@ CONTROL_SOCKET
Used while parsing Dhcp6/control-socket structures.
Definition dhcp6/parser_context.h:297
isc::dhcp::Parser6Context::SANITY_CHECKS
@ SANITY_CHECKS
Sanity checks.
Definition dhcp6/parser_context.h:247
isc::dhcp::Parser6Context::DHCP_QUEUE_CONTROL
@ DHCP_QUEUE_CONTROL
Used while parsing Dhcp6/dhcp-queue-control structures.
Definition dhcp6/parser_context.h:317
isc::dhcp::Parser6Context::SERVER_ID
@ SERVER_ID
Used while parsing Dhcp6/server-id structures.
Definition dhcp6/parser_context.h:291
isc::dhcp::Parser6Context::HOSTS_DATABASE
@ HOSTS_DATABASE
Used while parsing Dhcp6/hosts-database[s] structures.
Definition dhcp6/parser_context.h:253
isc::dhcp::Parser6Context::SUBNET6
@ SUBNET6
Used while parsing Dhcp6/Subnet6 structures.
Definition dhcp6/parser_context.h:271
isc::dhcp::Parser6Context::RESERVATIONS
@ RESERVATIONS
Used while parsing Dhcp6/reservations structures.
Definition dhcp6/parser_context.h:329
isc::dhcp::Parser6Context::CONFIG_DATABASE
@ CONFIG_DATABASE
Used while parsing config-control/config-databases.
Definition dhcp6/parser_context.h:356
isc::dhcp::Parser6Context::AUTH_TYPE
@ AUTH_TYPE
Used while parsing Dhcp6/control-socket/authentication/type structures.
Definition dhcp6/parser_context.h:307
isc::dhcp::Parser6Context::SSL_MODE
@ SSL_MODE
Used while parsing Dhcp6/*-database/ssl-mode.
Definition dhcp6/parser_context.h:259
isc::dhcp::Parser6Context::DHCP_DDNS
@ DHCP_DDNS
Used while parsing Dhcp6/dhcp-ddns.
Definition dhcp6/parser_context.h:341
isc::dhcp::Parser6Context::HTTP_HEADERS
@ HTTP_HEADERS
Definition dhcp6/parser_context.h:314
isc::dhcp::Parser6Context::COMPATIBILITY
@ COMPATIBILITY
Used while parsing compatibility parameters.
Definition dhcp6/parser_context.h:359
isc::dhcp::Parser6Context::INTERFACES_CONFIG
@ INTERFACES_CONFIG
Used while parsing Dhcp6/interfaces structures.
Definition dhcp6/parser_context.h:244
isc::dhcp::Parser6Context::DUID_TYPE
@ DUID_TYPE
Used while parsing Dhcp6/server-id/type structures.
Definition dhcp6/parser_context.h:294
isc::dhcp::Parser6Context::HOOKS_LIBRARIES
@ HOOKS_LIBRARIES
Used while parsing Dhcp6/hooks-libraries.
Definition dhcp6/parser_context.h:268
isc::dhcp::Parser6Context::CONFIG
@ CONFIG
Used while parsing content of Dhcp6.
Definition dhcp6/parser_context.h:238
isc::dhcp::Parser6Context::DDNS_CONFLICT_RESOLUTION_MODE
@ DDNS_CONFLICT_RESOLUTION_MODE
Used while parsing Dhcp6/ddns-conflict-resolution-mode.
Definition dhcp6/parser_context.h:362
isc::dhcp::Parser6Context::LEASE_DATABASE
@ LEASE_DATABASE
Used while parsing Dhcp6/lease-database structures.
Definition dhcp6/parser_context.h:250
isc::dhcp::Parser6Context::CLIENTS
@ CLIENTS
Used while parsing Dhcp6/control-socket/authentication/clients structures.
Definition dhcp6/parser_context.h:311
isc::dhcp::Parser6Context::NCR_PROTOCOL
@ NCR_PROTOCOL
Used while parsing Dhcp6/dhcp-ddns/ncr-protocol.
Definition dhcp6/parser_context.h:344
isc::dhcp::Parser6Context::RELAY
@ RELAY
Used while parsing Dhcp6/subnet6/relay structures.
Definition dhcp6/parser_context.h:332
isc::dhcp::Parser6Context::OUTPUT_OPTIONS
@ OUTPUT_OPTIONS
Used while parsing Dhcp6/loggers/output-options structures.
Definition dhcp6/parser_context.h:338
isc::dhcp::Parser6Context::HOST_RESERVATION_IDENTIFIERS
@ HOST_RESERVATION_IDENTIFIERS
Used while parsing Dhcp6/host-reservation-identifiers.
Definition dhcp6/parser_context.h:265
isc::dhcp::Parser6Context::NCR_FORMAT
@ NCR_FORMAT
Used while parsing Dhcp6/dhcp-ddns/ncr-format.
Definition dhcp6/parser_context.h:347
isc::dhcp::Parser6Context::REPLACE_CLIENT_NAME
@ REPLACE_CLIENT_NAME
Used while parsing Dhcp6/dhcp-ddns/replace-client-name.
Definition dhcp6/parser_context.h:350
isc::dhcp::Parser6Context::DHCP_MULTI_THREADING
@ DHCP_MULTI_THREADING
Used while parsing Dhcp6/multi-threading structures.
Definition dhcp6/parser_context.h:320
isc::dhcp::Parser6Context::NO_KEYWORD
@ NO_KEYWORD
This one is used in pure JSON mode.
Definition dhcp6/parser_context.h:235
isc::dhcp::Parser6Context::SHARED_NETWORK
@ SHARED_NETWORK
Used while parsing shared-networks structures.
Definition dhcp6/parser_context.h:274
isc::dhcp::Parser6Context::CONTROL_SOCKET_TYPE
@ CONTROL_SOCKET_TYPE
Used while parsing Dhcp6/control-socket/socket-type structures.
Definition dhcp6/parser_context.h:300
isc::dhcp::Parser6Context::PD_POOLS
@ PD_POOLS
Used while parsing Dhcp6/subnet6/pd-pools structures.
Definition dhcp6/parser_context.h:326
isc::dhcp::Parser6Context::MAC_SOURCES
@ MAC_SOURCES
Used while parsing Dhcp6/mac-sources structures.
Definition dhcp6/parser_context.h:262
isc::dhcp::Parser6Context::CONFIG_CONTROL
@ CONFIG_CONTROL
Used while parsing Dhcp6/config-control.
Definition dhcp6/parser_context.h:353
isc::dhcp::Parser6Context::warnAboutExtraCommas
void warnAboutExtraCommas(const isc::dhcp::location &loc)
Warning for extra commas.
Definition dhcp6/parser_context.cc:249
isc::dhcp::Parser6Context::scanEnd
void scanEnd()
Method called after the last tokens are scanned.
Definition dhcp6_lexer.cc:7288
isc::dhcp::Parser6Context::parseString
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
Definition dhcp6/parser_context.cc:31
isc::dhcp::Parser6Context::scanFileBegin
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
Definition dhcp6_lexer.cc:7266
isc::dhcp::Parser6Context::ParserType
ParserType
Defines currently supported scopes.
Definition dhcp6/parser_context.h:49
isc::dhcp::Parser6Context::contextName
const std::string contextName()
Get the syntactic context name.
Definition dhcp6/parser_context.cc:148
isc::dhcp::Parser6Context::ctx_
ParserContext ctx_
Current syntactic context.
Definition dhcp6/parser_context.h:394
data.h
parser_context.h
dhcp6_log.h
dhcp6_parser.h
Define the isc::dhcp::parser class.
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
LOG_WARN
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::db::fatal
@ fatal
Definition db_log.h:123
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::DHCP6_CONFIG_SYNTAX_WARNING
const isc::log::MessageID DHCP6_CONFIG_SYNTAX_WARNING
Definition dhcp6_messages.h:39
isc::dhcp::dhcp6_logger
isc::log::Logger dhcp6_logger(DHCP6_APP_LOGGER_NAME)
Base logger for DHCPv6 server.
Definition dhcp6_log.h:88
isc::util::file
Definition filesystem.cc:29
isc::util::str
Definition str.cc:29
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
isc::data::Element::Position
Represents the position of the data element within a configuration string.
Definition data.h:107