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-2019, 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 {
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  NDN_THROW(ConfigFile::Error("Unrecognized option face_system.netdev_bound." + key));
57  }
58  }
59  }
60 
61  if (context.isDryRun) {
62  return;
63  }
64 
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)) {
72  NDN_THROW(RuleParseError(i, "scheme '" + devScheme + "' for " +
73  remote.toString() + " is unavailable"));
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 
86 
88 }
89 
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  }
104  catch (const FaceUri::Error&) {
105  NDN_THROW_NESTED(RuleParseError(index, "invalid remote FaceUri"));
106  }
107  if (!rule.remotes.back().isCanonical()) {
108  NDN_THROW(RuleParseError(index, "remote FaceUri is not canonical"));
109  }
110  }
111  else if (key == "whitelist") {
112  if (hasWhitelist) {
113  NDN_THROW(RuleParseError(index, "duplicate whitelist"));
114  }
115  try {
116  rule.netifPredicate.parseWhitelist(value);
117  }
118  catch (const ConfigFile::Error&) {
119  NDN_THROW_NESTED(RuleParseError(index, "invalid whitelist"));
120  }
121  hasWhitelist = true;
122  }
123  else if (key == "blacklist") {
124  if (hasBlacklist) {
125  NDN_THROW(RuleParseError(index, "duplicate blacklist"));
126  }
127  try {
128  rule.netifPredicate.parseBlacklist(value);
129  }
130  catch (const ConfigFile::Error&) {
131  NDN_THROW_NESTED(RuleParseError(index, "invalid blacklist"));
132  }
133  hasBlacklist = true;
134  }
135  else {
136  NDN_THROW(RuleParseError(index, "unrecognized option " + key));
137  }
138  }
139 
140  if (rule.remotes.empty()) {
141  NDN_THROW(RuleParseError(index, "remote FaceUri is missing"));
142  }
143 
145  return rule;
146 }
147 
148 } // namespace face
149 } // namespace nfd
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:71
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context)
process face_system.netdev_bound config section
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
std::string toString() const
write as a string
Definition: face-uri.cpp:189
entry point of the face system
Definition: face-system.hpp:51
ndn security validator_config Rule
Definition: rule.cpp:28
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
#define NDN_THROW(e)
Definition: exception.hpp:61
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
manages netdev-bound faces
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
std::map< Key, shared_ptr< Face > > m_faces
std::vector< Rule > m_rules
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
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:109
Rule parseRule(int index, const ConfigSection &confRule) const