Kea: parser_context.cc Source File

| Kea 1.5.0

dhcp6/parser_context.cc
Go to the documentation of this file.
1 // Copyright (C) 2016-2018 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 <exceptions/exceptions.h>
12 #include <cc/data.h>
13 #include <boost/lexical_cast.hpp>
14 #include <fstream>
15 #include <limits>
16
17 namespace isc {
18 namespace dhcp {
19
20 Parser6Context::Parser6Context()
21 : ctx_(NO_KEYWORD), trace_scanning_(false), trace_parsing_(false)
22 {
23 }
24
25 Parser6Context::~Parser6Context()
26 {
27 }
28
29 isc::data::ElementPtr
30 Parser6Context::parseString(const std::string& str, ParserType parser_type)
31 {
32 scanStringBegin(str, parser_type);
33 return (parseCommon());
34 }
35
36 isc::data::ElementPtr
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
46 isc::data::ElementPtr
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
72 void
73 Parser6Context::error(const isc::dhcp::location& loc, const std::string& what)
74 {
75 isc_throw(Dhcp6ParseError, loc << ": " << what);
76 }
77
78 void
79 Parser6Context::error (const std::string& what)
80 {
81 isc_throw(Dhcp6ParseError, what);
82 }
83
84 void
85 Parser6Context::fatal (const std::string& what)
86 {
87 isc_throw(Dhcp6ParseError, what);
88 }
89
90 isc::data::Element::Position
91 Parser6Context::loc2pos(isc::dhcp::location& loc)
92 {
93 const std::string& file = *loc.begin.filename;
94 const uint32_t line = loc.begin.line;
95 const uint32_t pos = loc.begin.column;
96 return (isc::data::Element::Position(file, line, pos));
97 }
98
99 void
100 Parser6Context::require(const std::string& name,
101 isc::data::Element::Position open_loc,
102 isc::data::Element::Position close_loc)
103 {
104 ConstElementPtr value = stack_.back()->get(name);
105 if (!value) {
106 isc_throw(Dhcp6ParseError,
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::enter(const ParserContext& ctx)
116 {
117 cstack_.push_back(ctx_);
118 ctx_ = ctx;
119 }
120
121 void
122 Parser6Context::leave()
123 {
124 #if 1
125 if (cstack_.empty()) {
126 fatal("unbalanced syntactic context");
127 }
128 #endif
129 ctx_ = cstack_.back();
130 cstack_.pop_back();
131 }
132
133 const std::string
134 Parser6Context::contextName()
135 {
136 switch (ctx_) {
137 case NO_KEYWORD:
138 return ("__no keyword__");
139 case CONFIG:
140 return ("toplevel");
141 case DHCP6:
142 return ("Dhcp6");
143 case LOGGING:
144 return ("Logging");
145 case INTERFACES_CONFIG:
146 return ("interfaces-config");
147 case LEASE_DATABASE:
148 return ("lease-database");
149 case HOSTS_DATABASE:
150 return ("hosts-database");
151 case DATABASE_TYPE:
152 return ("database-type");
153 case MAC_SOURCES:
154 return ("mac-sources");
155 case HOST_RESERVATION_IDENTIFIERS:
156 return ("host-reservation-identifiers");
157 case HOOKS_LIBRARIES:
158 return ("hooks-libraries");
159 case SUBNET6:
160 return ("subnet6");
161 case RESERVATION_MODE:
162 return ("reservation-mode");
163 case OPTION_DEF:
164 return ("option-def");
165 case OPTION_DATA:
166 return ("option-data");
167 case CLIENT_CLASSES:
168 return ("client-classes");
169 case EXPIRED_LEASES_PROCESSING:
170 return ("expired-leases-processing");
171 case SERVER_ID:
172 return ("server-id");
173 case DUID_TYPE:
174 return ("duid-type");
175 case CONTROL_SOCKET:
176 return ("control-socket");
177 case DHCP_QUEUE_CONTROL:
178 return ("dhcp-queue-control");
179 case POOLS:
180 return ("pools");
181 case PD_POOLS:
182 return ("pd-pools");
183 case RESERVATIONS:
184 return ("reservations");
185 case RELAY:
186 return ("relay");
187 case LOGGERS:
188 return ("loggers");
189 case OUTPUT_OPTIONS:
190 return ("output-options");
191 case DHCP_DDNS:
192 return ("dhcp-ddns");
193 case NCR_PROTOCOL:
194 return ("ncr-protocol");
195 case NCR_FORMAT:
196 return ("ncr-format");
197 case REPLACE_CLIENT_NAME:
198 return ("replace-client-name");
199 case SHARED_NETWORK:
200 return ("shared-networks");
201 case SANITY_CHECKS:
202 return ("sanity-checks");
203 case CONFIG_CONTROL:
204 return ("config-control");
205 case CONFIG_DATABASE:
206 return ("config-database");
207 default:
208 return ("__unknown__");
209 }
210 }
211
212 };
213 };
dhcp6_parser.h
isc::dhcp::Parser6Context::RESERVATIONS
@ RESERVATIONS
Used while parsing Dhcp6/reservations structures.
Definition: dhcp6/parser_context.h:287
isc::dhcp::Parser6Context::Parser6Context
Parser6Context()
Default constructor.
Definition: dhcp6/parser_context.cc:20
isc::dhcp::Parser6Context::OPTION_DATA
@ OPTION_DATA
Used while parsing Dhcp6/option-data, Dhcp6/subnet6/option-data or anywhere option-data is present (c...
Definition: dhcp6/parser_context.h:260
isc::dhcp::Parser6Context::SHARED_NETWORK
@ SHARED_NETWORK
Used while parsing shared-networks structures.
Definition: dhcp6/parser_context.h:249
isc::dhcp::Parser6Context::CONFIG_DATABASE
@ CONFIG_DATABASE
Used while parsing config-control/config-databases.
Definition: dhcp6/parser_context.h:314
isc::dhcp::Parser6Context::CONTROL_SOCKET
@ CONTROL_SOCKET
Used while parsing Dhcp6/control-socket structures.
Definition: dhcp6/parser_context.h:275
isc::dhcp::Parser6Context::PD_POOLS
@ PD_POOLS
Used while parsing Dhcp6/subnet6/pd-pools structures.
Definition: dhcp6/parser_context.h:284
isc::dhcp::Parser6Context::contextName
const std::string contextName()
Get the syntactic context name.
Definition: dhcp6/parser_context.cc:134
isc::dhcp::Parser6Context::DATABASE_TYPE
@ DATABASE_TYPE
Used while parsing Dhcp6/*-database/type.
Definition: dhcp6/parser_context.h:234
isc::dhcp::Parser6Context::RELAY
@ RELAY
Used while parsing Dhcp6/subnet6/relay structures.
Definition: dhcp6/parser_context.h:290
isc::dhcp::Parser6Context::scanFileBegin
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
Definition: dhcp6_lexer.cc:5554
isc::dhcp::Parser6Context::ParserContext
ParserContext
Defines syntactic contexts for lexical tie-ins.
Definition: dhcp6/parser_context.h:206
isc::dhcp::Parser6Context::scanStringBegin
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
Definition: dhcp6_lexer.cc:5536
isc::dhcp::Parser6Context::DHCP6
@ DHCP6
Definition: dhcp6/parser_context.h:214
isc::dhcp::Parser6Context::NCR_PROTOCOL
@ NCR_PROTOCOL
Used while parsing Dhcp6/dhcp-ddns/ncr-protocol.
Definition: dhcp6/parser_context.h:302
isc::dhcp::Parser6Context::OUTPUT_OPTIONS
@ OUTPUT_OPTIONS
Used while parsing Logging/loggers/output_options structures.
Definition: dhcp6/parser_context.h:296
isc::dhcp::Parser6Context::require
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.
Definition: dhcp6/parser_context.cc:100
isc::dhcp::Parser6Context::ParserType
ParserType
Defines currently supported scopes.
Definition: dhcp6/parser_context.h:50
isc::dhcp::Parser6Context::~Parser6Context
virtual ~Parser6Context()
destructor
Definition: dhcp6/parser_context.cc:25
isc::dhcp::Parser6Context::HOST_RESERVATION_IDENTIFIERS
@ HOST_RESERVATION_IDENTIFIERS
Used while parsing Dhcp6/host-reservation-identifiers.
Definition: dhcp6/parser_context.h:240
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::dhcp::Parser6Context::REPLACE_CLIENT_NAME
@ REPLACE_CLIENT_NAME
Used while parsing Dhcp6/dhcp-ddns/replace-client-name.
Definition: dhcp6/parser_context.h:308
isc::dhcp::Parser6Context::CONFIG_CONTROL
@ CONFIG_CONTROL
Used while parsing Dhcp4/config-control.
Definition: dhcp6/parser_context.h:311
isc::dhcp::Parser6Context::error
void error(const isc::dhcp::location &loc, const std::string &what)
Error handler.
Definition: dhcp6/parser_context.cc:73
isc::dhcp::Parser6Context::stack_
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
Definition: dhcp6/parser_context.h:109
isc::dhcp::Parser6Context::parseFile
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
Definition: dhcp6/parser_context.cc:37
isc::dhcp::Parser6Context::DUID_TYPE
@ DUID_TYPE
Used while parsing Dhcp6/server-id/type structures.
Definition: dhcp6/parser_context.h:272
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::dhcp::Parser6Context::RESERVATION_MODE
@ RESERVATION_MODE
Used while parsing Dhcp6/reservation-mode.
Definition: dhcp6/parser_context.h:252
isc::data::Element::Position
Represents the position of the data element within a configuration string.
Definition: data.h:88
isc::dhcp::Parser6Context::SERVER_ID
@ SERVER_ID
Used while parsing Dhcp6/server-id structures.
Definition: dhcp6/parser_context.h:269
isc::dhcp::Parser6Context::enter
void enter(const ParserContext &ctx)
Enter a new syntactic context.
Definition: dhcp6/parser_context.cc:115
isc::dhcp::Parser6Context::DHCP_DDNS
@ DHCP_DDNS
Used while parsing Dhcp6/dhcp-ddns.
Definition: dhcp6/parser_context.h:299
isc::dhcp::Parser6Context::parseString
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
Definition: dhcp6/parser_context.cc:30
isc::dhcp::Parser6Context::HOSTS_DATABASE
@ HOSTS_DATABASE
Used while parsing Dhcp6/hosts-database[s] structures.
Definition: dhcp6/parser_context.h:231
isc::dhcp::Parser6Context::leave
void leave()
Leave a syntactic context.
Definition: dhcp6/parser_context.cc:122
isc::dhcp::Parser6Context::ctx_
ParserContext ctx_
Current syntactic context.
Definition: dhcp6/parser_context.h:347
isc::dhcp::Parser6Context::OPTION_DEF
@ OPTION_DEF
Used while parsing Dhcp6/option-def structures.
Definition: dhcp6/parser_context.h:255
parser_context.h
isc::dhcp::Parser6Context::scanEnd
void scanEnd()
Method called after the last tokens are scanned.
Definition: dhcp6_lexer.cc:5576
isc::dhcp::Parser6Context::NCR_FORMAT
@ NCR_FORMAT
Used while parsing Dhcp6/dhcp-ddns/ncr-format.
Definition: dhcp6/parser_context.h:305
isc::dhcp::Dhcp6Parser
A Bison parser.
Definition: dhcp6_parser.h:496
isc::dhcp::Parser6Context::MAC_SOURCES
@ MAC_SOURCES
Used while parsing Dhcp6/mac-sources structures.
Definition: dhcp6/parser_context.h:237
isc::dhcp::Parser6Context::DHCP_QUEUE_CONTROL
@ DHCP_QUEUE_CONTROL
Used while parsing Dhcp4/dhcp-queue-control structures.
Definition: dhcp6/parser_context.h:278
isc::dhcp::Parser6Context::LOGGERS
@ LOGGERS
Used while parsing Logging/loggers structures.
Definition: dhcp6/parser_context.h:293
isc::dhcp::Parser6Context::loc2pos
isc::data::Element::Position loc2pos(isc::dhcp::location &loc)
Converts bison's position to one understandable by isc::data::Element.
Definition: dhcp6/parser_context.cc:91
isc::dhcp::Parser6Context::HOOKS_LIBRARIES
@ HOOKS_LIBRARIES
Used while parsing Dhcp6/hooks-libraries.
Definition: dhcp6/parser_context.h:243
isc::dhcp::Parser6Context::CLIENT_CLASSES
@ CLIENT_CLASSES
Used while parsing Dhcp6/client-classes structures.
Definition: dhcp6/parser_context.h:263
data.h
exceptions.h
isc::dhcp::Parser6Context::SUBNET6
@ SUBNET6
Used while parsing Dhcp6/Subnet6 structures.
Definition: dhcp6/parser_context.h:246
isc::dhcp::Parser6Context::fatal
static void fatal(const std::string &what)
Fatal error handler.
Definition: dhcp6/parser_context.cc:85
isc::dhcp::Parser6Context::LOGGING
@ LOGGING
Used while parsing content of Logging.
Definition: dhcp6/parser_context.h:219
isc::dhcp::Parser6Context::CONFIG
@ CONFIG
Used while parsing content of Dhcp6.
Definition: dhcp6/parser_context.h:211
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
isc::dhcp::Parser6Context::INTERFACES_CONFIG
@ INTERFACES_CONFIG
Used while parsing Dhcp6/interfaces structures.
Definition: dhcp6/parser_context.h:222
isc::dhcp::Dhcp6ParseError
Evaluation error exception raised when trying to parse.
Definition: dhcp6/parser_context.h:28
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
isc::dhcp::Parser6Context::NO_KEYWORD
@ NO_KEYWORD
This one is used in pure JSON mode.
Definition: dhcp6/parser_context.h:208
isc::dhcp::Parser6Context::LEASE_DATABASE
@ LEASE_DATABASE
Used while parsing Dhcp6/lease-database structures.
Definition: dhcp6/parser_context.h:228
isc::dhcp::Parser6Context::EXPIRED_LEASES_PROCESSING
@ EXPIRED_LEASES_PROCESSING
Used while parsing Dhcp6/expired-leases-processing.
Definition: dhcp6/parser_context.h:266
isc::dhcp::Parser6Context::SANITY_CHECKS
@ SANITY_CHECKS
Sanity checks.
Definition: dhcp6/parser_context.h:225
isc::dhcp::Parser6Context::POOLS
@ POOLS
Used while parsing Dhcp6/subnet6/pools structures.
Definition: dhcp6/parser_context.h:281