NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2018, 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 "core/logger.hpp"
29 
30 namespace nfd {
31 namespace face {
32 
34 
36  : m_faceSystem(faceSystem)
37  , m_addFace(params.addFace)
38  , m_netmon(params.netmon)
39 {
40 }
41 
42 void
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 {
56  BOOST_THROW_EXCEPTION(ConfigFile::Error(
57  "Unrecognized option face_system.netdev_bound." + key));
58  }
59  }
60  }
61 
62  if (context.isDryRun) {
63  return;
64  }
65 
68  for (size_t i = 0; i < rules.size(); ++i) {
69  const Rule& rule = rules[i];
70  for (const FaceUri& remote : rule.remotes) {
71  std::string devScheme = remote.getScheme() + "+dev";
72  if (!m_faceSystem.hasFactoryForScheme(devScheme)) {
73  BOOST_THROW_EXCEPTION(RuleParseError(
74  i, "scheme " + devScheme + " for " + remote.toString() + " is unavailable"));
75  }
76  }
77  }
78  NFD_LOG_DEBUG("processConfig: processed " << rules.size() << " rules");
79 
80  m_rules.swap(rules);
81  std::map<Key, shared_ptr<Face>> oldFaces;
82  oldFaces.swap(m_faces);
83 
87 
89 }
90 
92 NetdevBound::parseRule(int index, const ConfigSection& confRule) const
93 {
94  Rule rule;
95 
96  bool hasWhitelist = false;
97  bool hasBlacklist = false;
98  for (const auto& pair : confRule) {
99  const std::string& key = pair.first;
100  const ConfigSection& value = pair.second;
101  if (key == "remote") {
102  try {
103  rule.remotes.emplace_back(value.get_value<std::string>());
104  }
105  catch (const FaceUri::Error& ex) {
106  BOOST_THROW_EXCEPTION(RuleParseError(index, "invalid remote FaceUri", ex.what()));
107  }
108  if (!rule.remotes.back().isCanonical()) {
109  BOOST_THROW_EXCEPTION(RuleParseError(index, "remote FaceUri is not canonical"));
110  }
111  }
112  else if (key == "whitelist") {
113  if (hasWhitelist) {
114  BOOST_THROW_EXCEPTION(RuleParseError(index, "duplicate whitelist"));
115  }
116  try {
117  rule.netifPredicate.parseWhitelist(value);
118  }
119  catch (const ConfigFile::Error& ex) {
120  BOOST_THROW_EXCEPTION(RuleParseError(index, "invalid whitelist", ex.what()));
121  }
122  hasWhitelist = true;
123  }
124  else if (key == "blacklist") {
125  if (hasBlacklist) {
126  BOOST_THROW_EXCEPTION(RuleParseError(index, "duplicate blacklist"));
127  }
128  try {
129  rule.netifPredicate.parseBlacklist(value);
130  }
131  catch (const ConfigFile::Error& ex) {
132  BOOST_THROW_EXCEPTION(RuleParseError(index, "invalid blacklist", ex.what()));
133  }
134  hasBlacklist = true;
135  }
136  else {
137  BOOST_THROW_EXCEPTION(RuleParseError(index, "unrecognized option " + key));
138  }
139  }
140 
141  if (rule.remotes.empty()) {
142  BOOST_THROW_EXCEPTION(RuleParseError(index, "remote FaceUri is missing"));
143  }
144 
146  return rule;
147 }
148 
149 } // namespace face
150 } // namespace nfd
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context)
process face_system.netdev_bound config section
std::string toString() const
write as a string
Definition: face-uri.cpp:204
entry point of the face system
Definition: face-system.hpp:51
ndn security validator_config Rule
Definition: rule.cpp:27
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
manages netdev-bound faces
Parameters to ProtocolFactory constructor.
boost::property_tree::ptree ConfigSection
a config file section
NetdevBound(const ProtocolFactoryCtorParams &params, const FaceSystem &faceSystem)
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
bool hasFactoryForScheme(const std::string &scheme) const
Definition: face-system.cpp:87
context for processing a config section in ProtocolFactory
Definition: face-system.hpp:96
const std::string & getScheme() const
get scheme (protocol)
Definition: face-uri.hpp:113
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31