blob: 3decef8ab3e004059d098a57dbb96692aea2671b [file] [log] [blame]
Junxiao Shieef49a92018-11-10 12:19:36 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040030namespace nfd::face {
Junxiao Shieef49a92018-11-10 12:19:36 +000031
32NFD_LOG_INIT(NetdevBound);
33
34NetdevBound::NetdevBound(const ProtocolFactoryCtorParams& params, const FaceSystem& faceSystem)
35 : m_faceSystem(faceSystem)
36 , m_addFace(params.addFace)
37 , m_netmon(params.netmon)
38{
39}
40
41void
42NetdevBound::processConfig(OptionalConfigSection configSection,
43 FaceSystem::ConfigContext& context)
44{
45 std::vector<Rule> rules;
46 if (configSection) {
47 int ruleIndex = 0;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040048 for (const auto& [key, value] : *configSection) {
Junxiao Shieef49a92018-11-10 12:19:36 +000049 if (key == "rule") {
50 rules.push_back(parseRule(ruleIndex++, value));
51 }
52 else {
Davide Pesavento19779d82019-02-14 13:40:04 -050053 NDN_THROW(ConfigFile::Error("Unrecognized option face_system.netdev_bound." + key));
Junxiao Shieef49a92018-11-10 12:19:36 +000054 }
55 }
56 }
57
58 if (context.isDryRun) {
59 return;
60 }
61
62 ///\todo #3521 this should be verified in dry-run, but PFs won't publish their *+dev schemes
63 /// in dry-run
64 for (size_t i = 0; i < rules.size(); ++i) {
65 const Rule& rule = rules[i];
66 for (const FaceUri& remote : rule.remotes) {
67 std::string devScheme = remote.getScheme() + "+dev";
68 if (!m_faceSystem.hasFactoryForScheme(devScheme)) {
Davide Pesavento19779d82019-02-14 13:40:04 -050069 NDN_THROW(RuleParseError(i, "scheme '" + devScheme + "' for " +
70 remote.toString() + " is unavailable"));
Junxiao Shieef49a92018-11-10 12:19:36 +000071 }
72 }
73 }
74 NFD_LOG_DEBUG("processConfig: processed " << rules.size() << " rules");
75
76 m_rules.swap(rules);
77 std::map<Key, shared_ptr<Face>> oldFaces;
78 oldFaces.swap(m_faces);
79
80 ///\todo #3521 for each face needed under m_rules:
81 /// if it's in oldFaces, add to m_faces and remove from oldFaces;
82 /// otherwise, create via factory and add to m_faces
83
84 ///\todo #3521 close faces in oldFaces
85}
86
87NetdevBound::Rule
88NetdevBound::parseRule(int index, const ConfigSection& confRule) const
89{
90 Rule rule;
91
92 bool hasWhitelist = false;
93 bool hasBlacklist = false;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040094 for (const auto& [key, value] : confRule) {
Junxiao Shieef49a92018-11-10 12:19:36 +000095 if (key == "remote") {
96 try {
97 rule.remotes.emplace_back(value.get_value<std::string>());
98 }
Davide Pesavento19779d82019-02-14 13:40:04 -050099 catch (const FaceUri::Error&) {
100 NDN_THROW_NESTED(RuleParseError(index, "invalid remote FaceUri"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000101 }
102 if (!rule.remotes.back().isCanonical()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500103 NDN_THROW(RuleParseError(index, "remote FaceUri is not canonical"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000104 }
105 }
106 else if (key == "whitelist") {
107 if (hasWhitelist) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500108 NDN_THROW(RuleParseError(index, "duplicate whitelist"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000109 }
110 try {
111 rule.netifPredicate.parseWhitelist(value);
112 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500113 catch (const ConfigFile::Error&) {
114 NDN_THROW_NESTED(RuleParseError(index, "invalid whitelist"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000115 }
116 hasWhitelist = true;
117 }
118 else if (key == "blacklist") {
119 if (hasBlacklist) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500120 NDN_THROW(RuleParseError(index, "duplicate blacklist"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000121 }
122 try {
123 rule.netifPredicate.parseBlacklist(value);
124 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500125 catch (const ConfigFile::Error&) {
126 NDN_THROW_NESTED(RuleParseError(index, "invalid blacklist"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000127 }
128 hasBlacklist = true;
129 }
130 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500131 NDN_THROW(RuleParseError(index, "unrecognized option " + key));
Junxiao Shieef49a92018-11-10 12:19:36 +0000132 }
133 }
134
135 if (rule.remotes.empty()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500136 NDN_THROW(RuleParseError(index, "remote FaceUri is missing"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000137 }
138
139 ///\todo #3521 for each remote, check that there is a factory providing scheme+dev scheme
140 return rule;
141}
142
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400143} // namespace nfd::face