Kea: parser_context.cc Source File

| Kea 2.2.0

dhcp6/parser_context.cc
Go to the documentation of this file.
1// Copyright (C) 2016-2021 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 }
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
83 Parser6Context::error(const std::string& what) {
84 isc_throw(Dhcp6ParseError, what);
85}
86
87void
88Parser6Context::fatal(const std::string& what) {
89 isc_throw(Dhcp6ParseError, what);
90}
91
92 isc::data::Element::Position
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,
102 isc::data::Element::Position open_loc,
103 isc::data::Element::Position close_loc) {
104 ConstElementPtr value = stack_.back()->get(name);
105 if (!value) {
106 isc_throw(Dhcp6ParseError,
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,
116 isc::data::Element::Position loc) {
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
132Parser6Context::enter(const ParserContext& ctx) {
133 cstack_.push_back(ctx_);
134 ctx_ = ctx;
135}
136
137void
138Parser6Context::leave() {
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
149Parser6Context::contextName() {
150 switch (ctx_) {
151 case NO_KEYWORD:
152 return ("__no keyword__");
153 case CONFIG:
154 return ("toplevel");
155 case DHCP6:
156 return ("Dhcp6");
157 case INTERFACES_CONFIG:
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");
169 case HOST_RESERVATION_IDENTIFIERS:
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");
183 case EXPIRED_LEASES_PROCESSING:
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");
191 case DHCP_QUEUE_CONTROL:
192 return ("dhcp-queue-control");
193 case DHCP_MULTI_THREADING:
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");
213 case REPLACE_CLIENT_NAME:
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");
225 default:
226 return ("__unknown__");
227 }
228}
229
230void
231Parser6Context::warning(const isc::dhcp::location& loc,
232 const std::string& what) {
233 std::ostringstream msg;
234 msg << loc << ": " << what;
235 LOG_WARN(dhcp6_logger, DHCP6_CONFIG_SYNTAX_WARNING)
236 .arg(msg.str());
237}
238
239void
240Parser6Context::warnAboutExtraCommas(const isc::dhcp::location& loc) {
241 warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
242}
243
244} // namespace dhcp
245} // 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:101
isc::dhcp::Parser6Context::fatal
static void fatal(const std::string &what)
Fatal error handler.
Definition: dhcp6/parser_context.cc:88
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:93
isc::dhcp::Parser6Context::~Parser6Context
virtual ~Parser6Context()
destructor
Definition: dhcp6/parser_context.cc:27
isc::dhcp::Parser6Context::warning
void warning(const isc::dhcp::location &loc, const std::string &what)
Warning handler.
Definition: dhcp6/parser_context.cc:231
isc::dhcp::Parser6Context::leave
void leave()
Leave a syntactic context.
Definition: dhcp6/parser_context.cc:138
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:115
isc::dhcp::Parser6Context::enter
void enter(const ParserContext &ctx)
Enter a new syntactic context.
Definition: dhcp6/parser_context.cc:132
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:72
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:6529
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:321
isc::dhcp::Parser6Context::RESERVATION_MODE
@ RESERVATION_MODE
Used while parsing Dhcp6/reservation-mode.
Definition: dhcp6/parser_context.h:277
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:288
isc::dhcp::Parser6Context::DATABASE_ON_FAIL
@ DATABASE_ON_FAIL
Used while parsing Dhcp6/*-database/on-fail.
Definition: dhcp6/parser_context.h:259
isc::dhcp::Parser6Context::OPTION_DEF
@ OPTION_DEF
Used while parsing Dhcp6/option-def structures.
Definition: dhcp6/parser_context.h:280
isc::dhcp::Parser6Context::POOLS
@ POOLS
Used while parsing Dhcp6/subnet6/pools structures.
Definition: dhcp6/parser_context.h:309
isc::dhcp::Parser6Context::EXPIRED_LEASES_PROCESSING
@ EXPIRED_LEASES_PROCESSING
Used while parsing Dhcp6/expired-leases-processing.
Definition: dhcp6/parser_context.h:291
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:285
isc::dhcp::Parser6Context::CONTROL_SOCKET
@ CONTROL_SOCKET
Used while parsing Dhcp6/control-socket structures.
Definition: dhcp6/parser_context.h:300
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:303
isc::dhcp::Parser6Context::SERVER_ID
@ SERVER_ID
Used while parsing Dhcp6/server-id structures.
Definition: dhcp6/parser_context.h:294
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:315
isc::dhcp::Parser6Context::CONFIG_DATABASE
@ CONFIG_DATABASE
Used while parsing config-control/config-databases.
Definition: dhcp6/parser_context.h:342
isc::dhcp::Parser6Context::DATABASE_TYPE
@ DATABASE_TYPE
Used while parsing Dhcp6/*-database/type.
Definition: dhcp6/parser_context.h:256
isc::dhcp::Parser6Context::DHCP_DDNS
@ DHCP_DDNS
Used while parsing Dhcp6/dhcp-ddns.
Definition: dhcp6/parser_context.h:327
isc::dhcp::Parser6Context::COMPATIBILITY
@ COMPATIBILITY
Used while parsing compatibility parameters.
Definition: dhcp6/parser_context.h:345
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:297
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::LEASE_DATABASE
@ LEASE_DATABASE
Used while parsing Dhcp6/lease-database structures.
Definition: dhcp6/parser_context.h:250
isc::dhcp::Parser6Context::NCR_PROTOCOL
@ NCR_PROTOCOL
Used while parsing Dhcp6/dhcp-ddns/ncr-protocol.
Definition: dhcp6/parser_context.h:330
isc::dhcp::Parser6Context::RELAY
@ RELAY
Used while parsing Dhcp6/subnet6/relay structures.
Definition: dhcp6/parser_context.h:318
isc::dhcp::Parser6Context::OUTPUT_OPTIONS
@ OUTPUT_OPTIONS
Used while parsing Dhcp6/loggers/output_options structures.
Definition: dhcp6/parser_context.h:324
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:333
isc::dhcp::Parser6Context::REPLACE_CLIENT_NAME
@ REPLACE_CLIENT_NAME
Used while parsing Dhcp6/dhcp-ddns/replace-client-name.
Definition: dhcp6/parser_context.h:336
isc::dhcp::Parser6Context::DHCP_MULTI_THREADING
@ DHCP_MULTI_THREADING
Used while parsing Dhcp6/multi-threading structures.
Definition: dhcp6/parser_context.h:306
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::PD_POOLS
@ PD_POOLS
Used while parsing Dhcp6/subnet6/pd-pools structures.
Definition: dhcp6/parser_context.h:312
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:339
isc::dhcp::Parser6Context::warnAboutExtraCommas
void warnAboutExtraCommas(const isc::dhcp::location &loc)
Warning for extra commas.
Definition: dhcp6/parser_context.cc:240
isc::dhcp::Parser6Context::scanEnd
void scanEnd()
Method called after the last tokens are scanned.
Definition: dhcp6_lexer.cc:6569
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:6547
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:149
isc::dhcp::Parser6Context::ctx_
ParserContext ctx_
Current syntactic context.
Definition: dhcp6/parser_context.h:378
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:27
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
isc::dhcp::DHCP6_CONFIG_SYNTAX_WARNING
const isc::log::MessageID DHCP6_CONFIG_SYNTAX_WARNING
Definition: dhcp6_messages.h:31
isc::dhcp::dhcp6_logger
isc::log::Logger dhcp6_logger(DHCP6_APP_LOGGER_NAME)
Base logger for DHCPv6 server.
Definition: dhcp6_log.h:88
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143
isc::data::Element::Position
Represents the position of the data element within a configuration string.
Definition: data.h:92