blob: b22bf627abef5c1af94630f26f7a9a0c199cca99 [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
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;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040049 for (const auto& [key, value] : *configSection) {
Junxiao Shieef49a92018-11-10 12:19:36 +000050 if (key == "rule") {
51 rules.push_back(parseRule(ruleIndex++, value));
52 }
53 else {
Davide Pesavento19779d82019-02-14 13:40:04 -050054 NDN_THROW(ConfigFile::Error("Unrecognized option face_system.netdev_bound." + key));
Junxiao Shieef49a92018-11-10 12:19:36 +000055 }
56 }
57 }
58
59 if (context.isDryRun) {
60 return;
61 }
62
63 ///\todo #3521 this should be verified in dry-run, but PFs won't publish their *+dev schemes
64 /// in dry-run
65 for (size_t i = 0; i < rules.size(); ++i) {
66 const Rule& rule = rules[i];
67 for (const FaceUri& remote : rule.remotes) {
68 std::string devScheme = remote.getScheme() + "+dev";
69 if (!m_faceSystem.hasFactoryForScheme(devScheme)) {
Davide Pesavento19779d82019-02-14 13:40:04 -050070 NDN_THROW(RuleParseError(i, "scheme '" + devScheme + "' for " +
71 remote.toString() + " is unavailable"));
Junxiao Shieef49a92018-11-10 12:19:36 +000072 }
73 }
74 }
75 NFD_LOG_DEBUG("processConfig: processed " << rules.size() << " rules");
76
77 m_rules.swap(rules);
78 std::map<Key, shared_ptr<Face>> oldFaces;
79 oldFaces.swap(m_faces);
80
81 ///\todo #3521 for each face needed under m_rules:
82 /// if it's in oldFaces, add to m_faces and remove from oldFaces;
83 /// otherwise, create via factory and add to m_faces
84
85 ///\todo #3521 close faces in oldFaces
86}
87
88NetdevBound::Rule
89NetdevBound::parseRule(int index, const ConfigSection& confRule) const
90{
91 Rule rule;
92
93 bool hasWhitelist = false;
94 bool hasBlacklist = false;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040095 for (const auto& [key, value] : confRule) {
Junxiao Shieef49a92018-11-10 12:19:36 +000096 if (key == "remote") {
97 try {
98 rule.remotes.emplace_back(value.get_value<std::string>());
99 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500100 catch (const FaceUri::Error&) {
101 NDN_THROW_NESTED(RuleParseError(index, "invalid remote FaceUri"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000102 }
103 if (!rule.remotes.back().isCanonical()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500104 NDN_THROW(RuleParseError(index, "remote FaceUri is not canonical"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000105 }
106 }
107 else if (key == "whitelist") {
108 if (hasWhitelist) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500109 NDN_THROW(RuleParseError(index, "duplicate whitelist"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000110 }
111 try {
112 rule.netifPredicate.parseWhitelist(value);
113 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500114 catch (const ConfigFile::Error&) {
115 NDN_THROW_NESTED(RuleParseError(index, "invalid whitelist"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000116 }
117 hasWhitelist = true;
118 }
119 else if (key == "blacklist") {
120 if (hasBlacklist) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500121 NDN_THROW(RuleParseError(index, "duplicate blacklist"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000122 }
123 try {
124 rule.netifPredicate.parseBlacklist(value);
125 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500126 catch (const ConfigFile::Error&) {
127 NDN_THROW_NESTED(RuleParseError(index, "invalid blacklist"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000128 }
129 hasBlacklist = true;
130 }
131 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500132 NDN_THROW(RuleParseError(index, "unrecognized option " + key));
Junxiao Shieef49a92018-11-10 12:19:36 +0000133 }
134 }
135
136 if (rule.remotes.empty()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500137 NDN_THROW(RuleParseError(index, "remote FaceUri is missing"));
Junxiao Shieef49a92018-11-10 12:19:36 +0000138 }
139
140 ///\todo #3521 for each remote, check that there is a factory providing scheme+dev scheme
141 return rule;
142}
143
144} // namespace face
145} // namespace nfd