Kea 2.5.8
dhcp4/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 <dhcp4/dhcp4_parser.h>
11#include <dhcp4/dhcp4_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
31Parser4Context::parseString(const std::string& str, ParserType parser_type) {
32 scanStringBegin(str, parser_type);
33 return (parseCommon());
34}
35
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
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 }
59 catch (...) {
60 scanEnd();
61 throw;
62 }
63 if (stack_.size() == 1) {
64 return (stack_[0]);
65 } else {
66 isc_throw(Dhcp4ParseError, "Expected exactly one terminal Element expected, found "
67 << stack_.size());
68 }
69}
70
71void
72Parser4Context::error(const isc::dhcp::location& loc,
73 const std::string& what,
74 size_t pos) {
75 if (pos == 0) {
76 isc_throw(Dhcp4ParseError, loc << ": " << what);
77 } else {
78 isc_throw(Dhcp4ParseError, loc << " (near " << pos << "): " << what);
79 }
80}
81
82void
83Parser4Context::error(const std::string& what) {
85}
86
87void
88Parser4Context::fatal(const std::string& what) {
90}
91
93Parser4Context::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
101Parser4Context::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
115Parser4Context::unique(const std::string& name,
117 ConstElementPtr value = stack_.back()->get(name);
118 if (value) {
119 if (ctx_ != NO_KEYWORD) {
120 isc_throw(Dhcp4ParseError, loc << ": duplicate " << name
121 << " entries in " << contextName()
122 << " map (previous at " << value->getPosition() << ")");
123 } else {
124 isc_throw(Dhcp4ParseError, loc << ": duplicate " << name
125 << " entries in JSON"
126 << " map (previous at " << value->getPosition() << ")");
127 }
128
129 }
130}
131
132void
134 cstack_.push_back(ctx_);
135 ctx_ = ctx;
136}
137
138void
140#if 1
141 if (cstack_.empty()) {
142 fatal("unbalanced syntactic context");
143 }
144#endif
145 ctx_ = cstack_.back();
146 cstack_.pop_back();
147}
148
149const std::string
151 switch (ctx_) {
152 case NO_KEYWORD:
153 return ("__no keyword__");
154 case CONFIG:
155 return ("toplevel");
156 case DHCP4:
157 return ("Dhcp4");
159 return ("interfaces-config");
160 case DHCP_SOCKET_TYPE:
161 return ("dhcp-socket-type");
163 return ("outbound-interface");
164 case LEASE_DATABASE:
165 return ("lease-database");
166 case HOSTS_DATABASE:
167 return ("hosts-database");
168 case DATABASE_TYPE:
169 return ("database-type");
170 case DATABASE_ON_FAIL:
171 return ("database-on-fail");
173 return ("host-reservation-identifiers");
174 case HOOKS_LIBRARIES:
175 return ("hooks-libraries");
176 case SUBNET4:
177 return ("subnet4");
178 case RESERVATION_MODE:
179 return ("reservation-mode");
180 case OPTION_DEF:
181 return ("option-def");
182 case OPTION_DATA:
183 return ("option-data");
184 case CLIENT_CLASSES:
185 return ("client-classes");
187 return ("expired-leases-processing");
188 case SERVER_ID:
189 return ("server-id");
190 case CONTROL_SOCKET:
191 return ("control-socket");
193 return ("dhcp-queue-control");
195 return ("multi-threading");
196 case POOLS:
197 return ("pools");
198 case RESERVATIONS:
199 return ("reservations");
200 case RELAY:
201 return ("relay");
202 case LOGGERS:
203 return ("loggers");
204 case OUTPUT_OPTIONS:
205 return ("output-options");
206 case DHCP_DDNS:
207 return ("dhcp-ddns");
208 case NCR_PROTOCOL:
209 return ("ncr-protocol");
210 case NCR_FORMAT:
211 return ("ncr-format");
213 return ("replace-client-name");
214 case SHARED_NETWORK:
215 return ("shared-networks");
216 case SANITY_CHECKS:
217 return ("sanity-checks");
218 case CONFIG_CONTROL:
219 return ("config-control");
220 case CONFIG_DATABASE:
221 return ("config-database");
222 case COMPATIBILITY:
223 return ("compatibility");
225 return ("ddns-conflict-resolution-mode");
226 default:
227 return ("__unknown__");
228 }
229}
230
231void
232Parser4Context::warning(const isc::dhcp::location& loc,
233 const std::string& what) {
234 std::ostringstream msg;
235 msg << loc << ": " << what;
237 .arg(msg.str());
238}
239
240void
241Parser4Context::warnAboutExtraCommas(const isc::dhcp::location& loc) {
242 warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
243}
244
245} // namespace dhcp
246} // namespace isc
Evaluation error exception raised when trying to parse.
A Bison parser.
Definition: dhcp4_parser.h:216
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
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.
isc::data::Element::Position loc2pos(isc::dhcp::location &loc)
Converts bison's position to one understandable by isc::data::Element.
void leave()
Leave a syntactic context.
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
void unique(const std::string &name, isc::data::Element::Position loc)
Check if a parameter is already present.
void scanEnd()
Method called after the last tokens are scanned.
void enter(const ParserContext &ctx)
Enter a new syntactic context.
Parser4Context()
Default constructor.
void warning(const isc::dhcp::location &loc, const std::string &what)
Warning handler.
void warnAboutExtraCommas(const isc::dhcp::location &loc)
Warning for extra commas.
const std::string contextName()
Get the syntactic context name.
ParserContext
Defines syntactic contexts for lexical tie-ins.
@ SUBNET4
Used while parsing Dhcp4/Subnet4 structures.
@ DATABASE_TYPE
Used while parsing Dhcp4/*-database/type.
@ POOLS
Used while parsing Dhcp4/subnet4/pools structures.
@ CLIENT_CLASSES
Used while parsing Dhcp4/client-classes structures.
@ RESERVATIONS
Used while parsing Dhcp4/reservations structures.
@ HOSTS_DATABASE
Used while parsing Dhcp4/hosts-database[s] structures.
@ NCR_PROTOCOL
Used while parsing Dhcp4/dhcp-ddns/ncr-protocol.
@ LOGGERS
Used while parsing Dhcp4/loggers structures.
@ DATABASE_ON_FAIL
Used while parsing Dhcp4/*-database/on-fail.
@ NCR_FORMAT
Used while parsing Dhcp4/dhcp-ddns/ncr-format.
@ SERVER_ID
Used while parsing Dhcp4/server-id structures.
@ DDNS_CONFLICT_RESOLUTION_MODE
Used while parsing Dhcp4/ib-ddns-conflict-resolution-mode.
@ COMPATIBILITY
Used while parsing compatibility parameters.
@ OUTBOUND_INTERFACE
Used while parsing Dhcp4/interfaces/outbound-interface structures.
@ CONFIG
Used while parsing content of Dhcp4.
@ OUTPUT_OPTIONS
Used while parsing Dhcp4/loggers/output-options structures.
@ RESERVATION_MODE
Used while parsing Dhcp4/reservation-mode.
@ CONTROL_SOCKET
Used while parsing Dhcp4/control-socket structures.
@ DHCP_DDNS
Used while parsing Dhcp4/dhcp-ddns.
@ OPTION_DATA
Used while parsing Dhcp4/option-data, Dhcp4/subnet4/option-data or anywhere option-data is present (c...
@ DHCP_MULTI_THREADING
Used while parsing Dhcp4/multi-threading structures.
@ LEASE_DATABASE
Used while parsing Dhcp4/lease-database structures.
@ SHARED_NETWORK
Used while parsing shared-networks structures.
@ EXPIRED_LEASES_PROCESSING
Used while parsing Dhcp4/expired-leases-processing.
@ CONFIG_CONTROL
Used while parsing Dhcp4/config-control.
@ INTERFACES_CONFIG
Used while parsing Dhcp4/interfaces structures.
@ HOST_RESERVATION_IDENTIFIERS
Used while parsing Dhcp4/host-reservation-identifiers.
@ OPTION_DEF
Used while parsing Dhcp4/option-def structures.
@ CONFIG_DATABASE
Used while parsing config-control/config-databases.
@ NO_KEYWORD
This one is used in pure JSON mode.
@ HOOKS_LIBRARIES
Used while parsing Dhcp4/hooks-libraries.
@ DHCP_QUEUE_CONTROL
Used while parsing Dhcp4/dhcp-queue-control structures.
@ REPLACE_CLIENT_NAME
Used while parsing Dhcp4/dhcp-ddns/replace-client-name.
@ RELAY
Used while parsing Dhcp4/subnet4relay structures.
@ DHCP_SOCKET_TYPE
Used while parsing Dhcp4/interfaces/dhcp-socket-type structures.
ParserType
Defines currently supported scopes.
static void fatal(const std::string &what)
Fatal error handler.
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
ParserContext ctx_
Current syntactic context.
void error(const isc::dhcp::location &loc, const std::string &what, size_t pos=0)
Error handler.
virtual ~Parser4Context()
destructor
Contains declarations for loggers used by the DHCPv4 server component.
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 DHCP4_CONFIG_SYNTAX_WARNING
isc::log::Logger dhcp4_logger(DHCP4_APP_LOGGER_NAME)
Base logger for DHCPv4 server.
Definition: dhcp4_log.h:90
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