Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 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" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 28 | #include "common/logger.hpp" |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 29 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 30 | namespace nfd::face { |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 31 | |
| 32 | NFD_LOG_INIT(NetdevBound); |
| 33 | |
| 34 | NetdevBound::NetdevBound(const ProtocolFactoryCtorParams& params, const FaceSystem& faceSystem) |
| 35 | : m_faceSystem(faceSystem) |
| 36 | , m_addFace(params.addFace) |
| 37 | , m_netmon(params.netmon) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | NetdevBound::processConfig(OptionalConfigSection configSection, |
| 43 | FaceSystem::ConfigContext& context) |
| 44 | { |
| 45 | std::vector<Rule> rules; |
| 46 | if (configSection) { |
| 47 | int ruleIndex = 0; |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 48 | for (const auto& [key, value] : *configSection) { |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 49 | if (key == "rule") { |
| 50 | rules.push_back(parseRule(ruleIndex++, value)); |
| 51 | } |
| 52 | else { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 53 | NDN_THROW(ConfigFile::Error("Unrecognized option face_system.netdev_bound." + key)); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 54 | } |
| 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 Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 69 | NDN_THROW(RuleParseError(i, "scheme '" + devScheme + "' for " + |
| 70 | remote.toString() + " is unavailable")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 71 | } |
| 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 | |
| 87 | NetdevBound::Rule |
| 88 | NetdevBound::parseRule(int index, const ConfigSection& confRule) const |
| 89 | { |
| 90 | Rule rule; |
| 91 | |
| 92 | bool hasWhitelist = false; |
| 93 | bool hasBlacklist = false; |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 94 | for (const auto& [key, value] : confRule) { |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 95 | if (key == "remote") { |
| 96 | try { |
| 97 | rule.remotes.emplace_back(value.get_value<std::string>()); |
| 98 | } |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 99 | catch (const FaceUri::Error&) { |
| 100 | NDN_THROW_NESTED(RuleParseError(index, "invalid remote FaceUri")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 101 | } |
| 102 | if (!rule.remotes.back().isCanonical()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 103 | NDN_THROW(RuleParseError(index, "remote FaceUri is not canonical")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | else if (key == "whitelist") { |
| 107 | if (hasWhitelist) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 108 | NDN_THROW(RuleParseError(index, "duplicate whitelist")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 109 | } |
| 110 | try { |
| 111 | rule.netifPredicate.parseWhitelist(value); |
| 112 | } |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 113 | catch (const ConfigFile::Error&) { |
| 114 | NDN_THROW_NESTED(RuleParseError(index, "invalid whitelist")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 115 | } |
| 116 | hasWhitelist = true; |
| 117 | } |
| 118 | else if (key == "blacklist") { |
| 119 | if (hasBlacklist) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 120 | NDN_THROW(RuleParseError(index, "duplicate blacklist")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 121 | } |
| 122 | try { |
| 123 | rule.netifPredicate.parseBlacklist(value); |
| 124 | } |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 125 | catch (const ConfigFile::Error&) { |
| 126 | NDN_THROW_NESTED(RuleParseError(index, "invalid blacklist")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 127 | } |
| 128 | hasBlacklist = true; |
| 129 | } |
| 130 | else { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 131 | NDN_THROW(RuleParseError(index, "unrecognized option " + key)); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | if (rule.remotes.empty()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 136 | NDN_THROW(RuleParseError(index, "remote FaceUri is missing")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | ///\todo #3521 for each remote, check that there is a factory providing scheme+dev scheme |
| 140 | return rule; |
| 141 | } |
| 142 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 143 | } // namespace nfd::face |