Kea 1.9.11
dhcp4/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 <dhcp4/dhcp4_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
29Parser4Context::parseString(const std::string& str, ParserType parser_type) {
30 scanStringBegin(str, parser_type);
31 return (parseCommon());
32}
33
35Parser4Context::parseFile(const std::string& filename, ParserType parser_type) {
36 FILE* f = fopen(filename.c_str(), "r");
37 if (!f) {
38 isc_throw(Dhcp4ParseError, "Unable to open file " << filename);
39 }
40 scanFileBegin(f, filename, parser_type);
41 return (parseCommon());
42}
43
45Parser4Context::parseCommon() {
46 isc::dhcp::Dhcp4Parser 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(Dhcp4ParseError, "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(Dhcp4ParseError, "Expected exactly one terminal Element expected, found "
65 << stack_.size());
66 }
67}
68
69void
70Parser4Context::error(const isc::dhcp::location& loc,
71 const std::string& what,
72 size_t pos) {
73 if (pos == 0) {
74 isc_throw(Dhcp4ParseError, loc << ": " << what);
75 } else {
76 isc_throw(Dhcp4ParseError, loc << " (near " << pos << "): " << what);
77 }
78}
79
80void
81Parser4Context::error(const std::string& what) {
83}
84
85void
86Parser4Context::fatal(const std::string& what) {
88}
89
91Parser4Context::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
99Parser4Context::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
113Parser4Context::unique(const std::string& name,
115 ConstElementPtr value = stack_.back()->get(name);
116 if (value) {
117 if (ctx_ != NO_KEYWORD) {
118 isc_throw(Dhcp4ParseError, loc << ": duplicate " << name
119 << " entries in " << contextName()
120 << " map (previous at " << value->getPosition() << ")");
121 } else {
122 isc_throw(Dhcp4ParseError, loc << ": duplicate " << name
123 << " entries in JSON"
124 << " map (previous at " << value->getPosition() << ")");
125 }
126
127 }
128}
129
130void
132 cstack_.push_back(ctx_);
133 ctx_ = ctx;
134}
135
136void
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
147const std::string
149 switch (ctx_) {
150 case NO_KEYWORD:
151 return ("__no keyword__");
152 case CONFIG:
153 return ("toplevel");
154 case DHCP4:
155 return ("Dhcp4");
157 return ("interfaces-config");
158 case DHCP_SOCKET_TYPE:
159 return ("dhcp-socket-type");
161 return ("outbound-interface");
162 case LEASE_DATABASE:
163 return ("lease-database");
164 case HOSTS_DATABASE:
165 return ("hosts-database");
166 case DATABASE_TYPE:
167 return ("database-type");
168 case DATABASE_ON_FAIL:
169 return ("database-on-fail");
171 return ("host-reservation-identifiers");
172 case HOOKS_LIBRARIES:
173 return ("hooks-libraries");
174 case SUBNET4:
175 return ("subnet4");
176 case RESERVATION_MODE:
177 return ("reservation-mode");
178 case OPTION_DEF:
179 return ("option-def");
180 case OPTION_DATA:
181 return ("option-data");
182 case CLIENT_CLASSES:
183 return ("client-classes");
185 return ("expired-leases-processing");
186 case SERVER_ID:
187 return ("server-id");
188 case CONTROL_SOCKET:
189 return ("control-socket");
191 return ("dhcp-queue-control");
193 return ("multi-threading");
194 case POOLS:
195 return ("pools");
196 case RESERVATIONS:
197 return ("reservations");
198 case RELAY:
199 return ("relay");
200 case LOGGERS:
201 return ("loggers");
202 case OUTPUT_OPTIONS:
203 return ("output-options");
204 case DHCP_DDNS:
205 return ("dhcp-ddns");
206 case NCR_PROTOCOL:
207 return ("ncr-protocol");
208 case NCR_FORMAT:
209 return ("ncr-format");
211 return ("replace-client-name");
212 case SHARED_NETWORK:
213 return ("shared-networks");
214 case SANITY_CHECKS:
215 return ("sanity-checks");
216 case CONFIG_CONTROL:
217 return ("config-control");
218 case CONFIG_DATABASE:
219 return ("config-database");
220 case COMPATIBILITY:
221 return ("compatibility");
222 default:
223 return ("__unknown__");
224 }
225}
226
227} // namespace dhcp
228} // namespace isc
Evaluation error exception raised when trying to parse.
A Bison parser.
Definition: dhcp4_parser.h:210
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.
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.
@ 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
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