Kea  2.1.7-git
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 
9 #include <dhcp6/parser_context.h>
10 #include <dhcp6/dhcp6_parser.h>
11 #include <dhcp6/dhcp6_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 
19 namespace isc {
20 namespace dhcp {
21 
23  : sfile_(nullptr), ctx_(NO_KEYWORD), trace_scanning_(false),
24  trace_parsing_(false) {
25 }
26 
28 }
29 
31 Parser6Context::parseString(const std::string& str, ParserType parser_type) {
32  scanStringBegin(str, parser_type);
33  return (parseCommon());
34 }
35 
37 Parser6Context::parseFile(const std::string& filename, ParserType parser_type) {
38  FILE* f = fopen(filename.c_str(), "r");
39  if (!f) {
40  isc_throw(Dhcp6ParseError, "Unable to open file " << filename);
41  }
42  scanFileBegin(f, filename, parser_type);
43  return (parseCommon());
44 }
45 
47 Parser6Context::parseCommon() {
48  isc::dhcp::Dhcp6Parser 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(Dhcp6ParseError, "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(Dhcp6ParseError, "Expected exactly one terminal Element expected, found "
67  << stack_.size());
68  }
69 }
70 
71 void
72 Parser6Context::error(const isc::dhcp::location& loc,
73  const std::string& what,
74  size_t pos) {
75  if (pos == 0) {
76  isc_throw(Dhcp6ParseError, loc << ": " << what);
77  } else {
78  isc_throw(Dhcp6ParseError, loc << " (near " << pos << "): " << what);
79  }
80 }
81 
82 void
83 Parser6Context::error(const std::string& what) {
85 }
86 
87 void
88 Parser6Context::fatal(const std::string& what) {
90 }
91 
93 Parser6Context::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 
100 void
101 Parser6Context::require(const std::string& name,
103  isc::data::Element::Position close_loc) {
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 
114 void
115 Parser6Context::unique(const std::string& name,
117  ConstElementPtr value = stack_.back()->get(name);
118  if (value) {
119  if (ctx_ != NO_KEYWORD) {
120  isc_throw(Dhcp6ParseError, loc << ": duplicate " << name
121  << " entries in " << contextName()
122  << " map (previous at " << value->getPosition() << ")");
123  } else {
124  isc_throw(Dhcp6ParseError, loc << ": duplicate " << name
125  << " entries in JSON"
126  << " map (previous at " << value->getPosition() << ")");
127  }
128  }
129 }
130 
131 void
133  cstack_.push_back(ctx_);
134  ctx_ = ctx;
135 }
136 
137 void
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 
148 const std::string
150  switch (ctx_) {
151  case NO_KEYWORD:
152  return ("__no keyword__");
153  case CONFIG:
154  return ("toplevel");
155  case DHCP6:
156  return ("Dhcp6");
157  case INTERFACES_CONFIG:
158  return ("interfaces-config");
159  case LEASE_DATABASE:
160  return ("lease-database");
161  case HOSTS_DATABASE:
162  return ("hosts-database");
163  case DATABASE_TYPE:
164  return ("database-type");
165  case DATABASE_ON_FAIL:
166  return ("database-on-fail");
167  case MAC_SOURCES:
168  return ("mac-sources");
170  return ("host-reservation-identifiers");
171  case HOOKS_LIBRARIES:
172  return ("hooks-libraries");
173  case SUBNET6:
174  return ("subnet6");
175  case RESERVATION_MODE:
176  return ("reservation-mode");
177  case OPTION_DEF:
178  return ("option-def");
179  case OPTION_DATA:
180  return ("option-data");
181  case CLIENT_CLASSES:
182  return ("client-classes");
184  return ("expired-leases-processing");
185  case SERVER_ID:
186  return ("server-id");
187  case DUID_TYPE:
188  return ("duid-type");
189  case CONTROL_SOCKET:
190  return ("control-socket");
191  case DHCP_QUEUE_CONTROL:
192  return ("dhcp-queue-control");
194  return ("multi-threading");
195  case POOLS:
196  return ("pools");
197  case PD_POOLS:
198  return ("pd-pools");
199  case RESERVATIONS:
200  return ("reservations");
201  case RELAY:
202  return ("relay");
203  case LOGGERS:
204  return ("loggers");
205  case OUTPUT_OPTIONS:
206  return ("output-options");
207  case DHCP_DDNS:
208  return ("dhcp-ddns");
209  case NCR_PROTOCOL:
210  return ("ncr-protocol");
211  case NCR_FORMAT:
212  return ("ncr-format");
213  case REPLACE_CLIENT_NAME:
214  return ("replace-client-name");
215  case SHARED_NETWORK:
216  return ("shared-networks");
217  case SANITY_CHECKS:
218  return ("sanity-checks");
219  case CONFIG_CONTROL:
220  return ("config-control");
221  case CONFIG_DATABASE:
222  return ("config-database");
223  case COMPATIBILITY:
224  return ("compatibility");
225  default:
226  return ("__unknown__");
227  }
228 }
229 
230 void
231 Parser6Context::warning(const isc::dhcp::location& loc,
232  const std::string& what) {
233  std::ostringstream msg;
234  msg << loc << ": " << what;
236  .arg(msg.str());
237 }
238 
239 void
240 Parser6Context::warnAboutExtraCommas(const isc::dhcp::location& loc) {
241  warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
242 }
243 
244 } // namespace dhcp
245 } // namespace isc
Used while parsing Dhcp6/lease-database structures.
ParserContext ctx_
Current syntactic context.
void warnAboutExtraCommas(const isc::dhcp::location &loc)
Warning for extra commas.
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition: macros.h:26
Used while parsing Dhcp6/option-def structures.
Define the isc::dhcp::parser class.
Used while parsing config-control/config-databases.
void enter(const ParserContext &ctx)
Enter a new syntactic context.
Used while parsing Dhcp6/client-classes structures.
Used while parsing Dhcp6/dhcp-queue-control structures.
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
Used while parsing Dhcp6/reservations structures.
void set_debug_level(debug_level_type l)
Set the current debugging level.
isc::data::Element::Position loc2pos(isc::dhcp::location &loc)
Converts bison&#39;s position to one understandable by isc::data::Element.
This one is used in pure JSON mode.
A Bison parser.
Definition: dhcp6_parser.h:215
Used while parsing Dhcp6/config-control.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
Used while parsing Dhcp6/dhcp-ddns/ncr-protocol.
void warning(const isc::dhcp::location &loc, const std::string &what)
Warning handler.
Used while parsing Dhcp6/dhcp-ddns/ncr-format.
virtual ~Parser6Context()
destructor
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
Used while parsing Dhcp6/*-database/on-fail.
Used while parsing Dhcp6/server-id/type structures.
Used while parsing Dhcp6/expired-leases-processing.
static void fatal(const std::string &what)
Fatal error handler.
Used while parsing Dhcp6/mac-sources structures.
Evaluation error exception raised when trying to parse.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Used while parsing Dhcp6/loggers/output_options structures.
Used while parsing content of Dhcp6.
ParserType
Defines currently supported scopes.
void leave()
Leave a syntactic context.
Used while parsing Dhcp6/*-database/type.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
const std::string contextName()
Get the syntactic context name.
Used while parsing Dhcp6/subnet6/pd-pools structures.
Used while parsing Dhcp6/option-data, Dhcp6/subnet6/option-data or anywhere option-data is present (c...
Used while parsing Dhcp6/hosts-database[s] structures.
Used while parsing Dhcp6/loggers structures.
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.
Parser6Context()
Default constructor.
void error(const isc::dhcp::location &loc, const std::string &what, size_t pos=0)
Error handler.
Represents the position of the data element within a configuration string.
Definition: data.h:92
Defines the logger used by the top-level component of kea-lfc.
Used while parsing Dhcp6/interfaces structures.
ParserContext
Defines syntactic contexts for lexical tie-ins.
Used while parsing shared-networks structures.
Used while parsing Dhcp6/dhcp-ddns/replace-client-name.
void unique(const std::string &name, isc::data::Element::Position loc)
Check if a parameter is already present.
Used while parsing Dhcp6/control-socket structures.
const isc::log::MessageID DHCP6_CONFIG_SYNTAX_WARNING
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.
Used while parsing Dhcp6/host-reservation-identifiers.
Used while parsing compatibility parameters.
Used while parsing Dhcp6/subnet6/relay structures.
virtual int parse()
Parse.
Used while parsing Dhcp6/multi-threading structures.
Used while parsing Dhcp6/subnet6/pools structures.
Used while parsing Dhcp6/reservation-mode.
Used while parsing Dhcp6/hooks-libraries.
isc::log::Logger dhcp6_logger(DHCP6_APP_LOGGER_NAME)
Base logger for DHCPv6 server.
Definition: dhcp6_log.h:88
Used while parsing Dhcp6/Subnet6 structures.
Used while parsing Dhcp6/dhcp-ddns.
Used while parsing Dhcp6/server-id structures.
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.