Kea 3.2.1-git
dhcp6/parser_context.cc
Go to the documentation of this file.
1// Copyright (C) 2016-2026 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 <dhcp6/dhcp6_parser.h>
11#include <dhcp6/dhcp6_log.h>
13#include <cc/data.h>
14#include <util/str.h>
15#include <boost/lexical_cast.hpp>
16#include <fstream>
17#include <sstream>
18#include <limits>
19
20namespace isc {
21namespace dhcp {
22
24 : sfile_(nullptr), ctx_(NO_KEYWORD), trace_scanning_(false),
25 trace_parsing_(false) {
26}
27
30
32Parser6Context::parseString(const std::string& str, ParserType parser_type) {
33 scanStringBegin(str, parser_type);
34 return (parseCommon());
35}
36
38Parser6Context::parseFile(const std::string& filename, ParserType parser_type) {
39 FILE* f = fopen(filename.c_str(), "r");
40 if (!f) {
41 isc_throw(Dhcp6ParseError, "Unable to open file " << filename);
42 }
43 scanFileBegin(f, filename, parser_type);
44 return (parseCommon());
45}
46
48Parser6Context::parseCommon() {
49 isc::dhcp::Dhcp6Parser parser(*this);
50 // Uncomment this to get detailed parser logs.
51 // trace_parsing_ = true;
52 parser.set_debug_level(trace_parsing_);
53 try {
54 int res = parser.parse();
55 if (res != 0) {
56 isc_throw(Dhcp6ParseError, "Parser abort");
57 }
58 scanEnd();
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, 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 std::string escaped = util::str::escapeNulls(what);
76 if (pos == 0) {
77 isc_throw(Dhcp6ParseError, loc << ": " << escaped);
78 } else {
79 isc_throw(Dhcp6ParseError, loc << " (near " << pos << "): " << escaped);
80 }
81}
82
83void
84Parser6Context::error(const std::string& what) {
86}
87
88void
89Parser6Context::fatal(const std::string& what) {
91}
92
94Parser6Context::loc2pos(isc::dhcp::location& loc) {
95 const std::string& file = *loc.begin.filename;
96 const uint32_t line = loc.begin.line;
97 const uint32_t pos = loc.begin.column;
98 return (isc::data::Element::Position(file, line, pos));
99}
100
101void
102Parser6Context::require(const std::string& name,
105 ConstElementPtr value = stack_.back()->get(name);
106 if (!value) {
108 "missing parameter '" << name << "' ("
109 << stack_.back()->getPosition() << ") ["
110 << contextName() << " map between "
111 << open_loc << " and " << close_loc << "]");
112 }
113}
114
115void
116Parser6Context::unique(const std::string& name,
118 ConstElementPtr value = stack_.back()->get(name);
119 if (value) {
120 if (ctx_ != NO_KEYWORD) {
121 isc_throw(Dhcp6ParseError, loc << ": duplicate " << name
122 << " entries in " << contextName()
123 << " map (previous at " << value->getPosition() << ")");
124 } else {
125 isc_throw(Dhcp6ParseError, loc << ": duplicate " << name
126 << " entries in JSON"
127 << " map (previous at " << value->getPosition() << ")");
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 DHCP6:
157 return ("Dhcp6");
159 return ("interfaces-config");
160 case LEASE_DATABASE:
161 return ("lease-database");
162 case HOSTS_DATABASE:
163 return ("hosts-database");
164 case DATABASE_ON_FAIL:
165 return ("database-on-fail");
166 case SSL_MODE:
167 return ("ssl-mode");
168 case MAC_SOURCES:
169 return ("mac-sources");
171 return ("host-reservation-identifiers");
172 case HOOKS_LIBRARIES:
173 return ("hooks-libraries");
174 case SUBNET6:
175 return ("subnet6");
176 case OPTION_DEF:
177 return ("option-def");
178 case OPTION_DATA:
179 return ("option-data");
180 case CLIENT_CLASSES:
181 return ("client-classes");
183 return ("expired-leases-processing");
184 case SERVER_ID:
185 return ("server-id");
186 case DUID_TYPE:
187 return ("duid-type");
188 case CONTROL_SOCKET:
189 return ("control-socket");
191 return ("control-socket-type");
192 case AUTHENTICATION:
193 return ("authentication");
194 case AUTH_TYPE:
195 return ("auth-type");
196 case CLIENTS:
197 return ("clients");
198 case HTTP_HEADERS:
199 return ("http-headers");
201 return ("dhcp-queue-control");
203 return ("multi-threading");
204 case POOLS:
205 return ("pools");
206 case PD_POOLS:
207 return ("pd-pools");
208 case RESERVATIONS:
209 return ("reservations");
210 case RELAY:
211 return ("relay");
212 case LOGGERS:
213 return ("loggers");
214 case OUTPUT_OPTIONS:
215 return ("output-options");
216 case DHCP_DDNS:
217 return ("dhcp-ddns");
218 case NCR_PROTOCOL:
219 return ("ncr-protocol");
220 case NCR_FORMAT:
221 return ("ncr-format");
223 return ("replace-client-name");
224 case SHARED_NETWORK:
225 return ("shared-networks");
226 case SANITY_CHECKS:
227 return ("sanity-checks");
228 case CONFIG_CONTROL:
229 return ("config-control");
230 case CONFIG_DATABASE:
231 return ("config-database");
232 case COMPATIBILITY:
233 return ("compatibility");
235 return ("ddns-conflict-resolution-mode");
236 default:
237 return ("__unknown__");
238 }
239}
240
241void
242Parser6Context::warning(const isc::dhcp::location& loc,
243 const std::string& what) {
244 std::ostringstream msg;
245 msg << loc << ": " << what;
247 .arg(msg.str());
248}
249
250void
251Parser6Context::warnAboutExtraCommas(const isc::dhcp::location& loc) {
252 warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
253}
254
255} // namespace dhcp
256} // namespace isc
Evaluation error exception raised when trying to parse.
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.
static void fatal(const std::string &what)
Fatal error handler.
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
isc::data::Element::Position loc2pos(isc::dhcp::location &loc)
Converts bison's position to one understandable by isc::data::Element.
virtual ~Parser6Context()
destructor
FILE * sfile_
sFile (aka FILE)
void warning(const isc::dhcp::location &loc, const std::string &what)
Warning handler.
void leave()
Leave a syntactic context.
void unique(const std::string &name, isc::data::Element::Position loc)
Check if a parameter is already present.
void enter(const ParserContext &ctx)
Enter a new syntactic context.
void error(const isc::dhcp::location &loc, const std::string &what, size_t pos=0)
Error handler.
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
Parser6Context()
Default constructor.
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
ParserContext
Defines syntactic contexts for lexical tie-ins.
@ LOGGERS
Used while parsing Dhcp6/loggers structures.
@ CLIENT_CLASSES
Used while parsing Dhcp6/client-classes structures.
@ DATABASE_ON_FAIL
Used while parsing Dhcp6/*-database/on-fail.
@ OPTION_DEF
Used while parsing Dhcp6/option-def structures.
@ POOLS
Used while parsing Dhcp6/subnet6/pools structures.
@ AUTHENTICATION
Used while parsing Dhcp6/control-socket/authentication structures.
@ EXPIRED_LEASES_PROCESSING
Used while parsing Dhcp6/expired-leases-processing.
@ OPTION_DATA
Used while parsing Dhcp6/option-data, Dhcp6/subnet6/option-data or anywhere option-data is present (c...
@ CONTROL_SOCKET
Used while parsing Dhcp6/control-socket structures.
@ DHCP_QUEUE_CONTROL
Used while parsing Dhcp6/dhcp-queue-control structures.
@ SERVER_ID
Used while parsing Dhcp6/server-id structures.
@ HOSTS_DATABASE
Used while parsing Dhcp6/hosts-database[s] structures.
@ SUBNET6
Used while parsing Dhcp6/Subnet6 structures.
@ RESERVATIONS
Used while parsing Dhcp6/reservations structures.
@ CONFIG_DATABASE
Used while parsing config-control/config-databases.
@ AUTH_TYPE
Used while parsing Dhcp6/control-socket/authentication/type structures.
@ SSL_MODE
Used while parsing Dhcp6/*-database/ssl-mode.
@ DHCP_DDNS
Used while parsing Dhcp6/dhcp-ddns.
@ COMPATIBILITY
Used while parsing compatibility parameters.
@ INTERFACES_CONFIG
Used while parsing Dhcp6/interfaces structures.
@ DUID_TYPE
Used while parsing Dhcp6/server-id/type structures.
@ HOOKS_LIBRARIES
Used while parsing Dhcp6/hooks-libraries.
@ CONFIG
Used while parsing content of Dhcp6.
@ DDNS_CONFLICT_RESOLUTION_MODE
Used while parsing Dhcp6/ddns-conflict-resolution-mode.
@ LEASE_DATABASE
Used while parsing Dhcp6/lease-database structures.
@ CLIENTS
Used while parsing Dhcp6/control-socket/authentication/clients structures.
@ NCR_PROTOCOL
Used while parsing Dhcp6/dhcp-ddns/ncr-protocol.
@ RELAY
Used while parsing Dhcp6/subnet6/relay structures.
@ OUTPUT_OPTIONS
Used while parsing Dhcp6/loggers/output-options structures.
@ HOST_RESERVATION_IDENTIFIERS
Used while parsing Dhcp6/host-reservation-identifiers.
@ NCR_FORMAT
Used while parsing Dhcp6/dhcp-ddns/ncr-format.
@ REPLACE_CLIENT_NAME
Used while parsing Dhcp6/dhcp-ddns/replace-client-name.
@ DHCP_MULTI_THREADING
Used while parsing Dhcp6/multi-threading structures.
@ NO_KEYWORD
This one is used in pure JSON mode.
@ SHARED_NETWORK
Used while parsing shared-networks structures.
@ CONTROL_SOCKET_TYPE
Used while parsing Dhcp6/control-socket/socket-type structures.
@ PD_POOLS
Used while parsing Dhcp6/subnet6/pd-pools structures.
@ MAC_SOURCES
Used while parsing Dhcp6/mac-sources structures.
@ CONFIG_CONTROL
Used while parsing Dhcp6/config-control.
void warnAboutExtraCommas(const isc::dhcp::location &loc)
Warning for extra commas.
void scanEnd()
Method called after the last tokens are scanned.
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
ParserType
Defines currently supported scopes.
const std::string contextName()
Get the syntactic context name.
ParserContext ctx_
Current syntactic context.
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:30
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
const isc::log::MessageID DHCP6_CONFIG_SYNTAX_WARNING
isc::log::Logger dhcp6_logger(DHCP6_APP_LOGGER_NAME)
Base logger for DHCPv6 server.
Definition dhcp6_log.h:88
std::string escapeNulls(std::string str)
Escape null characters.
Definition str.cc:432
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:107