blob: a47bd439b84eaab5e5d436497fe1fd563846ab28 [file] [log] [blame]
Junxiao Shieef49a92018-11-10 12:19:36 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "netdev-bound.hpp"
27#include "face-system.hpp"
28#include "core/logger.hpp"
29
30namespace nfd {
31namespace face {
32
33NFD_LOG_INIT(NetdevBound);
34
35NetdevBound::NetdevBound(const ProtocolFactoryCtorParams& params, const FaceSystem& faceSystem)
36 : m_faceSystem(faceSystem)
37 , m_addFace(params.addFace)
38 , m_netmon(params.netmon)
39{
40}
41
42void
43NetdevBound::processConfig(OptionalConfigSection configSection,
44 FaceSystem::ConfigContext& context)
45{
46 std::vector<Rule> rules;
47 if (configSection) {
48 int ruleIndex = 0;
49 for (const auto& pair : *configSection) {
50 const std::string& key = pair.first;
51 const ConfigSection& value = pair.second;
52 if (key == "rule") {
53 rules.push_back(parseRule(ruleIndex++, value));
54 }
55 else {
56 BOOST_THROW_EXCEPTION(ConfigFile::Error(
57 "Unrecognized option face_system.netdev_bound." + key));
58 }
59 }
60 }
61
62 if (context.isDryRun) {
63 return;
64 }
65
66 ///\todo #3521 this should be verified in dry-run, but PFs won't publish their *+dev schemes
67 /// in dry-run
68 for (size_t i = 0; i < rules.size(); ++i) {
69 const Rule& rule = rules[i];
70 for (const FaceUri& remote : rule.remotes) {
71 std::string devScheme = remote.getScheme() + "+dev";
72 if (!m_faceSystem.hasFactoryForScheme(devScheme)) {
73 BOOST_THROW_EXCEPTION(RuleParseError(
74 i, "scheme " + devScheme + " for " + remote.toString() + " is unavailable"));
75 }
76 }
77 }
78 NFD_LOG_DEBUG("processConfig: processed " << rules.size() << " rules");
79
80 m_rules.swap(rules);
81 std::map<Key, shared_ptr<Face>> oldFaces;
82 oldFaces.swap(m_faces);
83
84 ///\todo #3521 for each face needed under m_rules:
85 /// if it's in oldFaces, add to m_faces and remove from oldFaces;
86 /// otherwise, create via factory and add to m_faces
87
88 ///\todo #3521 close faces in oldFaces
89}
90
91NetdevBound::Rule
92NetdevBound::parseRule(int index, const ConfigSection& confRule) const
93{
94 Rule rule;
95
96 bool hasWhitelist = false;
97 bool hasBlacklist = false;
98 for (const auto& pair : confRule) {
99 const std::string& key = pair.first;
100 const ConfigSection& value = pair.second;
101 if (key == "remote") {
102 try {
103 rule.remotes.emplace_back(value.get_value<std::string>());
104 }
105 catch (const FaceUri::Error& ex) {
106 BOOST_THROW_EXCEPTION(RuleParseError(index, "invalid remote FaceUri", ex.what()));
107 }
108 if (!rule.remotes.back().isCanonical()) {
109 BOOST_THROW_EXCEPTION(RuleParseError(index, "remote FaceUri is not canonical"));
110 }
111 }
112 else if (key == "whitelist") {
113 if (hasWhitelist) {
114 BOOST_THROW_EXCEPTION(RuleParseError(index, "duplicate whitelist"));
115 }
116 try {
117 rule.netifPredicate.parseWhitelist(value);
118 }
119 catch (const ConfigFile::Error& ex) {
120 BOOST_THROW_EXCEPTION(RuleParseError(index, "invalid whitelist", ex.what()));
121 }
122 hasWhitelist = true;
123 }
124 else if (key == "blacklist") {
125 if (hasBlacklist) {
126 BOOST_THROW_EXCEPTION(RuleParseError(index, "duplicate blacklist"));
127 }
128 try {
129 rule.netifPredicate.parseBlacklist(value);
130 }
131 catch (const ConfigFile::Error& ex) {
132 BOOST_THROW_EXCEPTION(RuleParseError(index, "invalid blacklist", ex.what()));
133 }
134 hasBlacklist = true;
135 }
136 else {
137 BOOST_THROW_EXCEPTION(RuleParseError(index, "unrecognized option " + key));
138 }
139 }
140
141 if (rule.remotes.empty()) {
142 BOOST_THROW_EXCEPTION(RuleParseError(index, "remote FaceUri is missing"));
143 }
144
145 ///\todo #3521 for each remote, check that there is a factory providing scheme+dev scheme
146 return rule;
147}
148
149} // namespace face
150} // namespace nfd