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 | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | |
| 30 | namespace nfd { |
| 31 | namespace face { |
| 32 | |
| 33 | NFD_LOG_INIT(NetdevBound); |
| 34 | |
| 35 | NetdevBound::NetdevBound(const ProtocolFactoryCtorParams& params, const FaceSystem& faceSystem) |
| 36 | : m_faceSystem(faceSystem) |
| 37 | , m_addFace(params.addFace) |
| 38 | , m_netmon(params.netmon) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | NetdevBound::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 Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 56 | NDN_THROW(ConfigFile::Error("Unrecognized option face_system.netdev_bound." + key)); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 57 | } |
| 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 Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 72 | NDN_THROW(RuleParseError(i, "scheme '" + devScheme + "' for " + |
| 73 | remote.toString() + " is unavailable")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 74 | } |
| 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 | |
| 90 | NetdevBound::Rule |
| 91 | NetdevBound::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 Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 104 | catch (const FaceUri::Error&) { |
| 105 | NDN_THROW_NESTED(RuleParseError(index, "invalid remote FaceUri")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 106 | } |
| 107 | if (!rule.remotes.back().isCanonical()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 108 | NDN_THROW(RuleParseError(index, "remote FaceUri is not canonical")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | else if (key == "whitelist") { |
| 112 | if (hasWhitelist) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 113 | NDN_THROW(RuleParseError(index, "duplicate whitelist")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 114 | } |
| 115 | try { |
| 116 | rule.netifPredicate.parseWhitelist(value); |
| 117 | } |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 118 | catch (const ConfigFile::Error&) { |
| 119 | NDN_THROW_NESTED(RuleParseError(index, "invalid whitelist")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 120 | } |
| 121 | hasWhitelist = true; |
| 122 | } |
| 123 | else if (key == "blacklist") { |
| 124 | if (hasBlacklist) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 125 | NDN_THROW(RuleParseError(index, "duplicate blacklist")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 126 | } |
| 127 | try { |
| 128 | rule.netifPredicate.parseBlacklist(value); |
| 129 | } |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 130 | catch (const ConfigFile::Error&) { |
| 131 | NDN_THROW_NESTED(RuleParseError(index, "invalid blacklist")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 132 | } |
| 133 | hasBlacklist = true; |
| 134 | } |
| 135 | else { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 136 | NDN_THROW(RuleParseError(index, "unrecognized option " + key)); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | if (rule.remotes.empty()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 141 | NDN_THROW(RuleParseError(index, "remote FaceUri is missing")); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 142 | } |
| 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 |