blob: 124ea3a791a5772aec36116698453e3506d57e7e [file] [log] [blame]
Junxiao Shieef49a92018-11-10 12:19:36 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento19779d82019-02-14 13:40:04 -05003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shieef49a92018-11-10 12:19:36 +00004 * 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"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/logger.hpp"
Junxiao Shieef49a92018-11-10 12:19:36 +000029
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 {
Davide Pesavento19779d82019-02-14 13:40:04 -050056 NDN_THROW(ConfigFile::Error("Unrecognized option face_system.netdev_bound." + key));
Junxiao Shieef49a92018-11-10 12:19:36 +000057 }
58 }
59 }
60
61 if (context.isDryRun) {
62 return;
63 }
64
65 ///\todo #3521 this should be verified in dry-run, but PFs won't publish their *+dev schemes
66 /// in dry-run
67 for (size_t i = 0; i < rules.size(); ++i) {
68 const Rule& rule = rules[i];
69 for (const FaceUri& remote : rule.remotes) {
70 std::string devScheme = remote.getScheme() + "+dev";
71 if (!m_faceSystem.hasFactoryForScheme(devScheme)) {
Davide Pesavento19779d82019-02-14 13:40:04 -050072 NDN_THROW(RuleParseError(i, "scheme '" + devScheme + "' for " +
73 remote.toString() + " is unavailable"));
Junxiao Shieef49a92018-11-10 12:19:36 +000074 }
75 }
76 }
77 NFD_LOG_DEBUG("processConfig: processed " << rules.size() << " rules");
78
79 m_rules.swap(rules);
80 std::map<Key, shared_ptr<Face>> oldFaces;
81 oldFaces.swap(m_faces);
82
83 ///\todo #3521 for each face needed under m_rules:
84 /// if it's in oldFaces, add to m_faces and remove from oldFaces;
85 /// otherwise, create via factory and add to m_faces
86
87 ///\todo #3521 close faces in oldFaces
88}
89
90NetdevBound::Rule
91NetdevBound::parseRule(int index, const ConfigSection& confRule) const
92{
93 Rule rule;
94
95 bool hasWhitelist = false;
96 bool hasBlacklist = false;
97 for (const auto& pair : confRule) {
98 const std::string& key = pair.first;
99 const ConfigSection& value = pair.second;
100 if (key == "remote") {
101 try {
102 rule.remotes.emplace_back(value.get_value<std::string>());
103 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500104 catch (const FaceUri::Error&) {
105 NDN_THROW_NESTED(RuleParseError(index, "invalid remote FaceUri"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000106 }
107 if (!rule.remotes.back().isCanonical()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500108 NDN_THROW(RuleParseError(index, "remote FaceUri is not canonical"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000109 }
110 }
111 else if (key == "whitelist") {
112 if (hasWhitelist) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500113 NDN_THROW(RuleParseError(index, "duplicate whitelist"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000114 }
115 try {
116 rule.netifPredicate.parseWhitelist(value);
117 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500118 catch (const ConfigFile::Error&) {
119 NDN_THROW_NESTED(RuleParseError(index, "invalid whitelist"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000120 }
121 hasWhitelist = true;
122 }
123 else if (key == "blacklist") {
124 if (hasBlacklist) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500125 NDN_THROW(RuleParseError(index, "duplicate blacklist"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000126 }
127 try {
128 rule.netifPredicate.parseBlacklist(value);
129 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500130 catch (const ConfigFile::Error&) {
131 NDN_THROW_NESTED(RuleParseError(index, "invalid blacklist"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000132 }
133 hasBlacklist = true;
134 }
135 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500136 NDN_THROW(RuleParseError(index, "unrecognized option " + key));
Junxiao Shieef49a92018-11-10 12:19:36 +0000137 }
138 }
139
140 if (rule.remotes.empty()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500141 NDN_THROW(RuleParseError(index, "remote FaceUri is missing"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000142 }
143
144 ///\todo #3521 for each remote, check that there is a factory providing scheme+dev scheme
145 return rule;
146}
147
148} // namespace face
149} // namespace nfd