| Kea 3.0.2-git

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
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
19 namespace isc {
20 namespace dhcp {
21
22 Parser6Context::Parser6Context()
23 : sfile_(nullptr), ctx_(NO_KEYWORD), trace_scanning_(false),
24 trace_parsing_(false) {
25}
26
27 Parser6Context::~Parser6Context() {
28}
29
30 isc::data::ElementPtr
31 Parser6Context::parseString(const std::string& str, ParserType parser_type) {
32 scanStringBegin(str, parser_type);
33 return (parseCommon());
34}
35
36 isc::data::ElementPtr
37 Parser6Context::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
70 void
71 Parser6Context::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
81 void
82 Parser6Context::error(const std::string& what) {
83 isc_throw(Dhcp6ParseError, what);
84}
85
86 void
87 Parser6Context::fatal(const std::string& what) {
88 isc_throw(Dhcp6ParseError, what);
89}
90
91 isc::data::Element::Position
92 Parser6Context::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
99 void
100 Parser6Context::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
113 void
114 Parser6Context::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
130 void
131 Parser6Context::enter(const ParserContext& ctx) {
132 cstack_.push_back(ctx_);
133 ctx_ = ctx;
134}
135
136 void
137 Parser6Context::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
147 const std::string
148 Parser6Context::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 MAC_SOURCES:
165 return ("mac-sources");
166 case HOST_RESERVATION_IDENTIFIERS:
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");
178 case EXPIRED_LEASES_PROCESSING:
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");
186 case CONTROL_SOCKET_TYPE:
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");
194 case HTTP_HEADERS:
195 return ("http-headers");
196 case DHCP_QUEUE_CONTROL:
197 return ("dhcp-queue-control");
198 case DHCP_MULTI_THREADING:
199 return ("multi-threading");
200 case POOLS:
201 return ("pools");
202 case PD_POOLS:
203 return ("pd-pools");
204 case RESERVATIONS:
205 return ("reservations");
206 case RELAY:
207 return ("relay");
208 case LOGGERS:
209 return ("loggers");
210 case OUTPUT_OPTIONS:
211 return ("output-options");
212 case DHCP_DDNS:
213 return ("dhcp-ddns");
214 case NCR_PROTOCOL:
215 return ("ncr-protocol");
216 case NCR_FORMAT:
217 return ("ncr-format");
218 case REPLACE_CLIENT_NAME:
219 return ("replace-client-name");
220 case SHARED_NETWORK:
221 return ("shared-networks");
222 case SANITY_CHECKS:
223 return ("sanity-checks");
224 case CONFIG_CONTROL:
225 return ("config-control");
226 case CONFIG_DATABASE:
227 return ("config-database");
228 case COMPATIBILITY:
229 return ("compatibility");
230 case DDNS_CONFLICT_RESOLUTION_MODE:
231 return ("ddns-conflict-resolution-mode");
232 default:
233 return ("__unknown__");
234 }
235}
236
237 void
238 Parser6Context::warning(const isc::dhcp::location& loc,
239 const std::string& what) {
240 std::ostringstream msg;
241 msg << loc << ": " << what;
242 LOG_WARN(dhcp6_logger, DHCP6_CONFIG_SYNTAX_WARNING)
243 .arg(msg.str());
244}
245
246 void
247 Parser6Context::warnAboutExtraCommas(const isc::dhcp::location& loc) {
248 warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
249}
250
251} // namespace dhcp
252} // 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:381
isc::dhcp::Parser6Context::warning
void warning(const isc::dhcp::location &loc, const std::string &what)
Warning handler.
Definition dhcp6/parser_context.cc:238
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:7016
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:332
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:282
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:274
isc::dhcp::Parser6Context::POOLS
@ POOLS
Used while parsing Dhcp6/subnet6/pools structures.
Definition dhcp6/parser_context.h:320
isc::dhcp::Parser6Context::AUTHENTICATION
@ AUTHENTICATION
Used while parsing Dhcp6/control-socket/authentication structures.
Definition dhcp6/parser_context.h:300
isc::dhcp::Parser6Context::EXPIRED_LEASES_PROCESSING
@ EXPIRED_LEASES_PROCESSING
Used while parsing Dhcp6/expired-leases-processing.
Definition dhcp6/parser_context.h:285
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:279
isc::dhcp::Parser6Context::CONTROL_SOCKET
@ CONTROL_SOCKET
Used while parsing Dhcp6/control-socket structures.
Definition dhcp6/parser_context.h:294
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:314
isc::dhcp::Parser6Context::SERVER_ID
@ SERVER_ID
Used while parsing Dhcp6/server-id structures.
Definition dhcp6/parser_context.h:288
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:268
isc::dhcp::Parser6Context::RESERVATIONS
@ RESERVATIONS
Used while parsing Dhcp6/reservations structures.
Definition dhcp6/parser_context.h:326
isc::dhcp::Parser6Context::CONFIG_DATABASE
@ CONFIG_DATABASE
Used while parsing config-control/config-databases.
Definition dhcp6/parser_context.h:353
isc::dhcp::Parser6Context::AUTH_TYPE
@ AUTH_TYPE
Used while parsing Dhcp6/control-socket/authentication/type structures.
Definition dhcp6/parser_context.h:304
isc::dhcp::Parser6Context::DHCP_DDNS
@ DHCP_DDNS
Used while parsing Dhcp6/dhcp-ddns.
Definition dhcp6/parser_context.h:338
isc::dhcp::Parser6Context::HTTP_HEADERS
@ HTTP_HEADERS
Definition dhcp6/parser_context.h:311
isc::dhcp::Parser6Context::COMPATIBILITY
@ COMPATIBILITY
Used while parsing compatibility parameters.
Definition dhcp6/parser_context.h:356
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:291
isc::dhcp::Parser6Context::HOOKS_LIBRARIES
@ HOOKS_LIBRARIES
Used while parsing Dhcp6/hooks-libraries.
Definition dhcp6/parser_context.h:265
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:359
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:308
isc::dhcp::Parser6Context::NCR_PROTOCOL
@ NCR_PROTOCOL
Used while parsing Dhcp6/dhcp-ddns/ncr-protocol.
Definition dhcp6/parser_context.h:341
isc::dhcp::Parser6Context::RELAY
@ RELAY
Used while parsing Dhcp6/subnet6/relay structures.
Definition dhcp6/parser_context.h:329
isc::dhcp::Parser6Context::OUTPUT_OPTIONS
@ OUTPUT_OPTIONS
Used while parsing Dhcp6/loggers/output-options structures.
Definition dhcp6/parser_context.h:335
isc::dhcp::Parser6Context::HOST_RESERVATION_IDENTIFIERS
@ HOST_RESERVATION_IDENTIFIERS
Used while parsing Dhcp6/host-reservation-identifiers.
Definition dhcp6/parser_context.h:262
isc::dhcp::Parser6Context::NCR_FORMAT
@ NCR_FORMAT
Used while parsing Dhcp6/dhcp-ddns/ncr-format.
Definition dhcp6/parser_context.h:344
isc::dhcp::Parser6Context::REPLACE_CLIENT_NAME
@ REPLACE_CLIENT_NAME
Used while parsing Dhcp6/dhcp-ddns/replace-client-name.
Definition dhcp6/parser_context.h:347
isc::dhcp::Parser6Context::DHCP_MULTI_THREADING
@ DHCP_MULTI_THREADING
Used while parsing Dhcp6/multi-threading structures.
Definition dhcp6/parser_context.h:317
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:271
isc::dhcp::Parser6Context::CONTROL_SOCKET_TYPE
@ CONTROL_SOCKET_TYPE
Used while parsing Dhcp6/control-socket/socket-type structures.
Definition dhcp6/parser_context.h:297
isc::dhcp::Parser6Context::PD_POOLS
@ PD_POOLS
Used while parsing Dhcp6/subnet6/pd-pools structures.
Definition dhcp6/parser_context.h:323
isc::dhcp::Parser6Context::MAC_SOURCES
@ MAC_SOURCES
Used while parsing Dhcp6/mac-sources structures.
Definition dhcp6/parser_context.h:259
isc::dhcp::Parser6Context::CONFIG_CONTROL
@ CONFIG_CONTROL
Used while parsing Dhcp6/config-control.
Definition dhcp6/parser_context.h:350
isc::dhcp::Parser6Context::warnAboutExtraCommas
void warnAboutExtraCommas(const isc::dhcp::location &loc)
Warning for extra commas.
Definition dhcp6/parser_context.cc:247
isc::dhcp::Parser6Context::scanEnd
void scanEnd()
Method called after the last tokens are scanned.
Definition dhcp6_lexer.cc:7056
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:7034
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:391
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:29
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::db::fatal
@ fatal
Definition db_log.h:117
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:37
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:28
isc::util::str
Definition str.cc:29
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::data::Element::Position
Represents the position of the data element within a configuration string.
Definition data.h:94