Kea: parser_context.cc Source File

| Kea 3.2.0-git

dhcp4/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 <dhcp4/parser_context.h>
10#include <dhcp4/dhcp4_parser.h>
11#include <dhcp4/dhcp4_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
22Parser4Context::Parser4Context()
23 : sfile_(nullptr), ctx_(NO_KEYWORD), trace_scanning_(false),
24 trace_parsing_(false) {
25}
26
27Parser4Context::~Parser4Context() {
28}
29
30 isc::data::ElementPtr
31Parser4Context::parseString(const std::string& str, ParserType parser_type) {
32 scanStringBegin(str, parser_type);
33 return (parseCommon());
34}
35
36 isc::data::ElementPtr
37Parser4Context::parseFile(const std::string& filename, ParserType parser_type) {
38 FILE* f = fopen(filename.c_str(), "r");
39 if (!f) {
40 isc_throw(Dhcp4ParseError, "Unable to open file " << filename);
41 }
42 scanFileBegin(f, filename, parser_type);
43 return (parseCommon());
44}
45
46 isc::data::ElementPtr
47Parser4Context::parseCommon() {
48 isc::dhcp::Dhcp4Parser 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(Dhcp4ParseError, "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(Dhcp4ParseError, "Expected exactly one terminal Element, found "
66 << stack_.size());
67 }
68}
69
70void
71Parser4Context::error(const isc::dhcp::location& loc,
72 const std::string& what,
73 size_t pos) {
74 if (pos == 0) {
75 isc_throw(Dhcp4ParseError, loc << ": " << what);
76 } else {
77 isc_throw(Dhcp4ParseError, loc << " (near " << pos << "): " << what);
78 }
79}
80
81void
82 Parser4Context::error(const std::string& what) {
83 isc_throw(Dhcp4ParseError, what);
84}
85
86void
87Parser4Context::fatal(const std::string& what) {
88 isc_throw(Dhcp4ParseError, what);
89}
90
91 isc::data::Element::Position
92Parser4Context::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
100Parser4Context::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(Dhcp4ParseError,
106 "missing parameter '" << name << "' ("
107 << stack_.back()->getPosition() << ") ["
108 << contextName() << " map between "
109 << open_loc << " and " << close_loc << "]");
110 }
111}
112
113void
114Parser4Context::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(Dhcp4ParseError, loc << ": duplicate " << name
120 << " entries in " << contextName()
121 << " map (previous at " << value->getPosition() << ")");
122 } else {
123 isc_throw(Dhcp4ParseError, loc << ": duplicate " << name
124 << " entries in JSON"
125 << " map (previous at " << value->getPosition() << ")");
126 }
127
128 }
129}
130
131void
132Parser4Context::enter(const ParserContext& ctx) {
133 cstack_.push_back(ctx_);
134 ctx_ = ctx;
135}
136
137void
138Parser4Context::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
149Parser4Context::contextName() {
150 switch (ctx_) {
151 case NO_KEYWORD:
152 return ("__no keyword__");
153 case CONFIG:
154 return ("toplevel");
155 case DHCP4:
156 return ("Dhcp4");
157 case INTERFACES_CONFIG:
158 return ("interfaces-config");
159 case DHCP_SOCKET_TYPE:
160 return ("dhcp-socket-type");
161 case OUTBOUND_INTERFACE:
162 return ("outbound-interface");
163 case LEASE_DATABASE:
164 return ("lease-database");
165 case HOSTS_DATABASE:
166 return ("hosts-database");
167 case DATABASE_ON_FAIL:
168 return ("database-on-fail");
169 case SSL_MODE:
170 return ("ssl-mode");
171 case HOST_RESERVATION_IDENTIFIERS:
172 return ("host-reservation-identifiers");
173 case HOOKS_LIBRARIES:
174 return ("hooks-libraries");
175 case SUBNET4:
176 return ("subnet4");
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 CONTROL_SOCKET:
188 return ("control-socket");
189 case CONTROL_SOCKET_TYPE:
190 return ("control-socket-type");
191 case AUTHENTICATION:
192 return ("authentication");
193 case AUTH_TYPE:
194 return ("auth-type");
195 case CLIENTS:
196 return ("clients");
197 case HTTP_HEADERS:
198 return ("http-headers");
199 case DHCP_QUEUE_CONTROL:
200 return ("dhcp-queue-control");
201 case DHCP_MULTI_THREADING:
202 return ("multi-threading");
203 case POOLS:
204 return ("pools");
205 case RESERVATIONS:
206 return ("reservations");
207 case RELAY:
208 return ("relay");
209 case LOGGERS:
210 return ("loggers");
211 case OUTPUT_OPTIONS:
212 return ("output-options");
213 case DHCP_DDNS:
214 return ("dhcp-ddns");
215 case NCR_PROTOCOL:
216 return ("ncr-protocol");
217 case NCR_FORMAT:
218 return ("ncr-format");
219 case REPLACE_CLIENT_NAME:
220 return ("replace-client-name");
221 case SHARED_NETWORK:
222 return ("shared-networks");
223 case SANITY_CHECKS:
224 return ("sanity-checks");
225 case CONFIG_CONTROL:
226 return ("config-control");
227 case CONFIG_DATABASE:
228 return ("config-database");
229 case COMPATIBILITY:
230 return ("compatibility");
231 case DDNS_CONFLICT_RESOLUTION_MODE:
232 return ("ddns-conflict-resolution-mode");
233 default:
234 return ("__unknown__");
235 }
236}
237
238void
239Parser4Context::warning(const isc::dhcp::location& loc,
240 const std::string& what) {
241 std::ostringstream msg;
242 msg << loc << ": " << what;
243 LOG_WARN(dhcp4_logger, DHCP4_CONFIG_SYNTAX_WARNING)
244 .arg(msg.str());
245}
246
247void
248Parser4Context::warnAboutExtraCommas(const isc::dhcp::location& loc) {
249 warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
250}
251
252} // namespace dhcp
253} // namespace isc
isc::dhcp::Dhcp4ParseError
Evaluation error exception raised when trying to parse.
Definition dhcp4/parser_context.h:28
isc::dhcp::Dhcp4Parser
A Bison parser.
Definition dhcp4_parser.h:216
isc::dhcp::Parser4Context::scanFileBegin
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
Definition dhcp4_lexer.cc:7231
isc::dhcp::Parser4Context::parseFile
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
Definition dhcp4/parser_context.cc:37
isc::dhcp::Parser4Context::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 dhcp4/parser_context.cc:100
isc::dhcp::Parser4Context::loc2pos
isc::data::Element::Position loc2pos(isc::dhcp::location &loc)
Converts bison's position to one understandable by isc::data::Element.
Definition dhcp4/parser_context.cc:92
isc::dhcp::Parser4Context::leave
void leave()
Leave a syntactic context.
Definition dhcp4/parser_context.cc:138
isc::dhcp::Parser4Context::parseString
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
Definition dhcp4/parser_context.cc:31
isc::dhcp::Parser4Context::unique
void unique(const std::string &name, isc::data::Element::Position loc)
Check if a parameter is already present.
Definition dhcp4/parser_context.cc:114
isc::dhcp::Parser4Context::scanEnd
void scanEnd()
Method called after the last tokens are scanned.
Definition dhcp4_lexer.cc:7253
isc::dhcp::Parser4Context::enter
void enter(const ParserContext &ctx)
Enter a new syntactic context.
Definition dhcp4/parser_context.cc:132
isc::dhcp::Parser4Context::Parser4Context
Parser4Context()
Default constructor.
Definition dhcp4/parser_context.cc:22
isc::dhcp::Parser4Context::warning
void warning(const isc::dhcp::location &loc, const std::string &what)
Warning handler.
Definition dhcp4/parser_context.cc:239
isc::dhcp::Parser4Context::warnAboutExtraCommas
void warnAboutExtraCommas(const isc::dhcp::location &loc)
Warning for extra commas.
Definition dhcp4/parser_context.cc:248
isc::dhcp::Parser4Context::contextName
const std::string contextName()
Get the syntactic context name.
Definition dhcp4/parser_context.cc:149
isc::dhcp::Parser4Context::ParserContext
ParserContext
Defines syntactic contexts for lexical tie-ins.
Definition dhcp4/parser_context.h:230
isc::dhcp::Parser4Context::SUBNET4
@ SUBNET4
Used while parsing Dhcp4/Subnet4 structures.
Definition dhcp4/parser_context.h:271
isc::dhcp::Parser4Context::POOLS
@ POOLS
Used while parsing Dhcp4/subnet4/pools structures.
Definition dhcp4/parser_context.h:320
isc::dhcp::Parser4Context::CLIENT_CLASSES
@ CLIENT_CLASSES
Used while parsing Dhcp4/client-classes structures.
Definition dhcp4/parser_context.h:285
isc::dhcp::Parser4Context::RESERVATIONS
@ RESERVATIONS
Used while parsing Dhcp4/reservations structures.
Definition dhcp4/parser_context.h:323
isc::dhcp::Parser4Context::HOSTS_DATABASE
@ HOSTS_DATABASE
Used while parsing Dhcp4/hosts-database[s] structures.
Definition dhcp4/parser_context.h:256
isc::dhcp::Parser4Context::NCR_PROTOCOL
@ NCR_PROTOCOL
Used while parsing Dhcp4/dhcp-ddns/ncr-protocol.
Definition dhcp4/parser_context.h:338
isc::dhcp::Parser4Context::LOGGERS
@ LOGGERS
Used while parsing Dhcp4/loggers structures.
Definition dhcp4/parser_context.h:329
isc::dhcp::Parser4Context::DATABASE_ON_FAIL
@ DATABASE_ON_FAIL
Used while parsing Dhcp4/*-database/on-fail.
Definition dhcp4/parser_context.h:259
isc::dhcp::Parser4Context::NCR_FORMAT
@ NCR_FORMAT
Used while parsing Dhcp4/dhcp-ddns/ncr-format.
Definition dhcp4/parser_context.h:341
isc::dhcp::Parser4Context::SERVER_ID
@ SERVER_ID
Used while parsing Dhcp4/server-id structures.
Definition dhcp4/parser_context.h:291
isc::dhcp::Parser4Context::DDNS_CONFLICT_RESOLUTION_MODE
@ DDNS_CONFLICT_RESOLUTION_MODE
Used while parsing Dhcp4/ib-ddns-conflict-resolution-mode.
Definition dhcp4/parser_context.h:356
isc::dhcp::Parser4Context::COMPATIBILITY
@ COMPATIBILITY
Used while parsing compatibility parameters.
Definition dhcp4/parser_context.h:353
isc::dhcp::Parser4Context::OUTBOUND_INTERFACE
@ OUTBOUND_INTERFACE
Used while parsing Dhcp4/interfaces/outbound-interface structures.
Definition dhcp4/parser_context.h:250
isc::dhcp::Parser4Context::CONFIG
@ CONFIG
Used while parsing content of Dhcp4.
Definition dhcp4/parser_context.h:235
isc::dhcp::Parser4Context::OUTPUT_OPTIONS
@ OUTPUT_OPTIONS
Used while parsing Dhcp4/loggers/output-options structures.
Definition dhcp4/parser_context.h:332
isc::dhcp::Parser4Context::CONTROL_SOCKET
@ CONTROL_SOCKET
Used while parsing Dhcp4/control-socket structures.
Definition dhcp4/parser_context.h:294
isc::dhcp::Parser4Context::DHCP_DDNS
@ DHCP_DDNS
Used while parsing Dhcp4/dhcp-ddns.
Definition dhcp4/parser_context.h:335
isc::dhcp::Parser4Context::OPTION_DATA
@ OPTION_DATA
Used while parsing Dhcp4/option-data, Dhcp4/subnet4/option-data or anywhere option-data is present (c...
Definition dhcp4/parser_context.h:282
isc::dhcp::Parser4Context::AUTH_TYPE
@ AUTH_TYPE
Used while parsing Dhcp4/control-socket/authentication/type structures.
Definition dhcp4/parser_context.h:304
isc::dhcp::Parser4Context::DHCP_MULTI_THREADING
@ DHCP_MULTI_THREADING
Used while parsing Dhcp4/multi-threading structures.
Definition dhcp4/parser_context.h:317
isc::dhcp::Parser4Context::LEASE_DATABASE
@ LEASE_DATABASE
Used while parsing Dhcp4/lease-database structures.
Definition dhcp4/parser_context.h:253
isc::dhcp::Parser4Context::SHARED_NETWORK
@ SHARED_NETWORK
Used while parsing shared-networks structures.
Definition dhcp4/parser_context.h:274
isc::dhcp::Parser4Context::DHCP4
@ DHCP4
Definition dhcp4/parser_context.h:238
isc::dhcp::Parser4Context::EXPIRED_LEASES_PROCESSING
@ EXPIRED_LEASES_PROCESSING
Used while parsing Dhcp4/expired-leases-processing.
Definition dhcp4/parser_context.h:288
isc::dhcp::Parser4Context::HTTP_HEADERS
@ HTTP_HEADERS
Definition dhcp4/parser_context.h:311
isc::dhcp::Parser4Context::CLIENTS
@ CLIENTS
Used while parsing Dhcp4/control-socket/authentication/clients structures.
Definition dhcp4/parser_context.h:308
isc::dhcp::Parser4Context::SANITY_CHECKS
@ SANITY_CHECKS
Sanity checks.
Definition dhcp4/parser_context.h:244
isc::dhcp::Parser4Context::CONFIG_CONTROL
@ CONFIG_CONTROL
Used while parsing Dhcp4/config-control.
Definition dhcp4/parser_context.h:347
isc::dhcp::Parser4Context::INTERFACES_CONFIG
@ INTERFACES_CONFIG
Used while parsing Dhcp4/interfaces structures.
Definition dhcp4/parser_context.h:241
isc::dhcp::Parser4Context::HOST_RESERVATION_IDENTIFIERS
@ HOST_RESERVATION_IDENTIFIERS
Used while parsing Dhcp4/host-reservation-identifiers.
Definition dhcp4/parser_context.h:265
isc::dhcp::Parser4Context::CONTROL_SOCKET_TYPE
@ CONTROL_SOCKET_TYPE
Used while parsing Dhcp4/control-socket/socket-type structures.
Definition dhcp4/parser_context.h:297
isc::dhcp::Parser4Context::OPTION_DEF
@ OPTION_DEF
Used while parsing Dhcp4/option-def structures.
Definition dhcp4/parser_context.h:277
isc::dhcp::Parser4Context::CONFIG_DATABASE
@ CONFIG_DATABASE
Used while parsing config-control/config-databases.
Definition dhcp4/parser_context.h:350
isc::dhcp::Parser4Context::NO_KEYWORD
@ NO_KEYWORD
This one is used in pure JSON mode.
Definition dhcp4/parser_context.h:232
isc::dhcp::Parser4Context::HOOKS_LIBRARIES
@ HOOKS_LIBRARIES
Used while parsing Dhcp4/hooks-libraries.
Definition dhcp4/parser_context.h:268
isc::dhcp::Parser4Context::DHCP_QUEUE_CONTROL
@ DHCP_QUEUE_CONTROL
Used while parsing Dhcp4/dhcp-queue-control structures.
Definition dhcp4/parser_context.h:314
isc::dhcp::Parser4Context::AUTHENTICATION
@ AUTHENTICATION
Used while parsing Dhcp4/control-socket/authentication structures.
Definition dhcp4/parser_context.h:300
isc::dhcp::Parser4Context::REPLACE_CLIENT_NAME
@ REPLACE_CLIENT_NAME
Used while parsing Dhcp4/dhcp-ddns/replace-client-name.
Definition dhcp4/parser_context.h:344
isc::dhcp::Parser4Context::RELAY
@ RELAY
Used while parsing Dhcp4/subnet4relay structures.
Definition dhcp4/parser_context.h:326
isc::dhcp::Parser4Context::SSL_MODE
@ SSL_MODE
Used while parsing Dhcp4/*-database/ssl-mode.
Definition dhcp4/parser_context.h:262
isc::dhcp::Parser4Context::DHCP_SOCKET_TYPE
@ DHCP_SOCKET_TYPE
Used while parsing Dhcp4/interfaces/dhcp-socket-type structures.
Definition dhcp4/parser_context.h:247
isc::dhcp::Parser4Context::ParserType
ParserType
Defines currently supported scopes.
Definition dhcp4/parser_context.h:49
isc::dhcp::Parser4Context::fatal
static void fatal(const std::string &what)
Fatal error handler.
Definition dhcp4/parser_context.cc:87
isc::dhcp::Parser4Context::sfile_
FILE * sfile_
sFile (aka FILE)
Definition dhcp4/parser_context.h:378
isc::dhcp::Parser4Context::stack_
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
Definition dhcp4/parser_context.h:101
isc::dhcp::Parser4Context::scanStringBegin
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
Definition dhcp4_lexer.cc:7213
isc::dhcp::Parser4Context::ctx_
ParserContext ctx_
Current syntactic context.
Definition dhcp4/parser_context.h:388
isc::dhcp::Parser4Context::error
void error(const isc::dhcp::location &loc, const std::string &what, size_t pos=0)
Error handler.
Definition dhcp4/parser_context.cc:71
isc::dhcp::Parser4Context::~Parser4Context
virtual ~Parser4Context()
destructor
Definition dhcp4/parser_context.cc:27
data.h
parser_context.h
dhcp4_log.h
Contains declarations for loggers used by the DHCPv4 server component.
dhcp4_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::DHCP4_CONFIG_SYNTAX_WARNING
const isc::log::MessageID DHCP4_CONFIG_SYNTAX_WARNING
Definition dhcp4_messages.h:41
isc::dhcp::dhcp4_logger
isc::log::Logger dhcp4_logger(DHCP4_APP_LOGGER_NAME)
Base logger for DHCPv4 server.
Definition dhcp4_log.h:90
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