netdev-bound.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California,
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"
28 #include "common/logger.hpp"
29 
30 namespace nfd::face {
31 
32 NFD_LOG_INIT(NetdevBound);
33 
35  : m_faceSystem(faceSystem)
36  , m_addFace(params.addFace)
37  , m_netmon(params.netmon)
38 {
39 }
40 
41 void
44 {
45  std::vector<Rule> rules;
46  if (configSection) {
47  int ruleIndex = 0;
48  for (const auto& [key, value] : *configSection) {
49  if (key == "rule") {
50  rules.push_back(parseRule(ruleIndex++, value));
51  }
52  else {
53  NDN_THROW(ConfigFile::Error("Unrecognized option face_system.netdev_bound." + key));
54  }
55  }
56  }
57 
58  if (context.isDryRun) {
59  return;
60  }
61 
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)) {
69  NDN_THROW(RuleParseError(i, "scheme '" + devScheme + "' for " +
70  remote.toString() + " is unavailable"));
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 
83 
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;
94  for (const auto& [key, value] : confRule) {
95  if (key == "remote") {
96  try {
97  rule.remotes.emplace_back(value.get_value<std::string>());
98  }
99  catch (const FaceUri::Error&) {
100  NDN_THROW_NESTED(RuleParseError(index, "invalid remote FaceUri"));
101  }
102  if (!rule.remotes.back().isCanonical()) {
103  NDN_THROW(RuleParseError(index, "remote FaceUri is not canonical"));
104  }
105  }
106  else if (key == "whitelist") {
107  if (hasWhitelist) {
108  NDN_THROW(RuleParseError(index, "duplicate whitelist"));
109  }
110  try {
111  rule.netifPredicate.parseWhitelist(value);
112  }
113  catch (const ConfigFile::Error&) {
114  NDN_THROW_NESTED(RuleParseError(index, "invalid whitelist"));
115  }
116  hasWhitelist = true;
117  }
118  else if (key == "blacklist") {
119  if (hasBlacklist) {
120  NDN_THROW(RuleParseError(index, "duplicate blacklist"));
121  }
122  try {
123  rule.netifPredicate.parseBlacklist(value);
124  }
125  catch (const ConfigFile::Error&) {
126  NDN_THROW_NESTED(RuleParseError(index, "invalid blacklist"));
127  }
128  hasBlacklist = true;
129  }
130  else {
131  NDN_THROW(RuleParseError(index, "unrecognized option " + key));
132  }
133  }
134 
135  if (rule.remotes.empty()) {
136  NDN_THROW(RuleParseError(index, "remote FaceUri is missing"));
137  }
138 
140  return rule;
141 }
142 
143 } // namespace nfd::face
Context for processing a config section in ProtocolFactory.
Definition: face-system.hpp:99
Entry point of NFD's face system.
Definition: face-system.hpp:53
bool hasFactoryForScheme(const std::string &scheme) const
Definition: face-system.cpp:88
NetdevBound(const ProtocolFactoryCtorParams &params, const FaceSystem &faceSystem)
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context)
Process face_system.netdev_bound config section.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
boost::optional< const ConfigSection & > OptionalConfigSection
An optional configuration file section.
Definition: config-file.hpp:43
boost::property_tree::ptree ConfigSection
A configuration file section.
Definition: config-file.hpp:38
Parameters to ProtocolFactory constructor.