Kea 1.9.11
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
10#include <dhcp6/dhcp6_parser.h>
12#include <cc/data.h>
13#include <boost/lexical_cast.hpp>
14#include <fstream>
15#include <limits>
16
17namespace isc {
18namespace dhcp {
19
21 : sfile_(nullptr), ctx_(NO_KEYWORD), trace_scanning_(false),
22 trace_parsing_(false) {
23}
24
26}
27
29Parser6Context::parseString(const std::string& str, ParserType parser_type) {
30 scanStringBegin(str, parser_type);
31 return (parseCommon());
32}
33
35Parser6Context::parseFile(const std::string& filename, ParserType parser_type) {
36 FILE* f = fopen(filename.c_str(), "r");
37 if (!f) {
38 isc_throw(Dhcp6ParseError, "Unable to open file " << filename);
39 }
40 scanFileBegin(f, filename, parser_type);
41 return (parseCommon());
42}
43
45Parser6Context::parseCommon() {
46 isc::dhcp::Dhcp6Parser parser(*this);
47 // Uncomment this to get detailed parser logs.
48 // trace_parsing_ = true;
49 parser.set_debug_level(trace_parsing_);
50 try {
51 int res = parser.parse();
52 if (res != 0) {
53 isc_throw(Dhcp6ParseError, "Parser abort");
54 }
55 scanEnd();
56 }
57 catch (...) {
58 scanEnd();
59 throw;
60 }
61 if (stack_.size() == 1) {
62 return (stack_[0]);
63 } else {
64 isc_throw(Dhcp6ParseError, "Expected exactly one terminal Element expected, found "
65 << stack_.size());
66 }
67}
68
69void
70Parser6Context::error(const isc::dhcp::location& loc,
71 const std::string& what,
72 size_t pos) {
73 if (pos == 0) {
74 isc_throw(Dhcp6ParseError, loc << ": " << what);
75 } else {
76 isc_throw(Dhcp6ParseError, loc << " (near " << pos << "): " << what);
77 }
78}
79
80void
81Parser6Context::error(const std::string& what) {
83}
84
85void
86Parser6Context::fatal(const std::string& what) {
88}
89
91Parser6Context::loc2pos(isc::dhcp::location& loc) {
92 const std::string& file = *loc.begin.filename;
93 const uint32_t line = loc.begin.line;
94 const uint32_t pos = loc.begin.column;
95 return (isc::data::Element::Position(file, line, pos));
96}
97
98void
99Parser6Context::require(const std::string& name,
102 ConstElementPtr value = stack_.back()->get(name);
103 if (!value) {
105 "missing parameter '" << name << "' ("
106 << stack_.back()->getPosition() << ") ["
107 << contextName() << " map between "
108 << open_loc << " and " << close_loc << "]");
109 }
110}
111
112void
113Parser6Context::unique(const std::string& name,
115 ConstElementPtr value = stack_.back()->get(name);
116 if (value) {
117 if (ctx_ != NO_KEYWORD) {
118 isc_throw(Dhcp6ParseError, loc << ": duplicate " << name
119 << " entries in " << contextName()
120 << " map (previous at " << value->getPosition() << ")");
121 } else {
122 isc_throw(Dhcp6ParseError, loc << ": duplicate " << name
123 << " entries in JSON"
124 << " map (previous at " << value->getPosition() << ")");
125 }
126 }
127}
128
129void
131 cstack_.push_back(ctx_);
132 ctx_ = ctx;
133}
134
135void
137#if 1
138 if (cstack_.empty()) {
139 fatal("unbalanced syntactic context");
140 }
141#endif
142 ctx_ = cstack_.back();
143 cstack_.pop_back();
144}
145
146const std::string
148 switch (ctx_) {
149 case NO_KEYWORD:
150 return ("__no keyword__");
151 case CONFIG:
152 return ("toplevel");
153 case DHCP6:
154 return ("Dhcp6");
156 return ("interfaces-config");
157 case LEASE_DATABASE:
158 return ("lease-database");
159 case HOSTS_DATABASE:
160 return ("hosts-database");
161 case DATABASE_TYPE:
162 return ("database-type");
163 case DATABASE_ON_FAIL:
164 return ("database-on-fail");
165 case MAC_SOURCES:
166 return ("mac-sources");
168 return ("host-reservation-identifiers");
169 case HOOKS_LIBRARIES:
170 return ("hooks-libraries");
171 case SUBNET6:
172 return ("subnet6");
173 case RESERVATION_MODE:
174 return ("reservation-mode");
175 case OPTION_DEF:
176 return ("option-def");
177 case OPTION_DATA:
178 return ("option-data");
179 case CLIENT_CLASSES:
180 return ("client-classes");
182 return ("expired-leases-processing");
183 case SERVER_ID:
184 return ("server-id");
185 case DUID_TYPE:
186 return ("duid-type");
187 case CONTROL_SOCKET:
188 return ("control-socket");
190 return ("dhcp-queue-control");
192 return ("multi-threading");
193 case POOLS:
194 return ("pools");
195 case PD_POOLS:
196 return ("pd-pools");
197 case RESERVATIONS:
198 return ("reservations");
199 case RELAY:
200 return ("relay");
201 case LOGGERS:
202 return ("loggers");
203 case OUTPUT_OPTIONS:
204 return ("output-options");
205 case DHCP_DDNS:
206 return ("dhcp-ddns");
207 case NCR_PROTOCOL:
208 return ("ncr-protocol");
209 case NCR_FORMAT:
210 return ("ncr-format");
212 return ("replace-client-name");
213 case SHARED_NETWORK:
214 return ("shared-networks");
215 case SANITY_CHECKS:
216 return ("sanity-checks");
217 case CONFIG_CONTROL:
218 return ("config-control");
219 case CONFIG_DATABASE:
220 return ("config-database");
221 case COMPATIBILITY:
222 return ("compatibility");
223 default:
224 return ("__unknown__");
225 }
226}
227
228} // namespace dhcp
229} // namespace isc
Evaluation error exception raised when trying to parse.
A Bison parser.
Definition: dhcp6_parser.h:210
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
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.
@ RESERVATION_MODE
Used while parsing Dhcp6/reservation-mode.
@ 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.
@ 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.
@ DATABASE_TYPE
Used while parsing Dhcp6/*-database/type.
@ 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.
@ LEASE_DATABASE
Used while parsing Dhcp6/lease-database 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.
@ 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 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.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
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:92