Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [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 "face-manager.hpp" |
| 27 | |
| 28 | #include "core/network-interface.hpp" |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 29 | #include "face/generic-link-service.hpp" |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 30 | #include "face/tcp-factory.hpp" |
| 31 | #include "face/udp-factory.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 32 | #include "fw/face-table.hpp" |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 33 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 34 | #include <ndn-cxx/management/nfd-channel-status.hpp> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 35 | #include <ndn-cxx/management/nfd-face-status.hpp> |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 36 | #include <ndn-cxx/management/nfd-face-event-notification.hpp> |
| 37 | |
| 38 | #ifdef HAVE_UNIX_SOCKETS |
| 39 | #include "face/unix-stream-factory.hpp" |
| 40 | #endif // HAVE_UNIX_SOCKETS |
| 41 | |
| 42 | #ifdef HAVE_LIBPCAP |
| 43 | #include "face/ethernet-factory.hpp" |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 44 | #include "face/ethernet-transport.hpp" |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 45 | #endif // HAVE_LIBPCAP |
| 46 | |
| 47 | #ifdef HAVE_WEBSOCKET |
| 48 | #include "face/websocket-factory.hpp" |
| 49 | #endif // HAVE_WEBSOCKET |
| 50 | |
| 51 | namespace nfd { |
| 52 | |
| 53 | NFD_LOG_INIT("FaceManager"); |
| 54 | |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 55 | FaceManager::FaceManager(FaceTable& faceTable, Dispatcher& dispatcher, CommandAuthenticator& authenticator) |
| 56 | : NfdManagerBase(dispatcher, authenticator, "faces") |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 57 | , m_faceTable(faceTable) |
| 58 | { |
| 59 | registerCommandHandler<ndn::nfd::FaceCreateCommand>("create", |
| 60 | bind(&FaceManager::createFace, this, _2, _3, _4, _5)); |
| 61 | |
| 62 | registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy", |
| 63 | bind(&FaceManager::destroyFace, this, _2, _3, _4, _5)); |
| 64 | |
| 65 | registerCommandHandler<ndn::nfd::FaceEnableLocalControlCommand>("enable-local-control", |
| 66 | bind(&FaceManager::enableLocalControl, this, _2, _3, _4, _5)); |
| 67 | |
| 68 | registerCommandHandler<ndn::nfd::FaceDisableLocalControlCommand>("disable-local-control", |
| 69 | bind(&FaceManager::disableLocalControl, this, _2, _3, _4, _5)); |
| 70 | |
| 71 | registerStatusDatasetHandler("list", bind(&FaceManager::listFaces, this, _1, _2, _3)); |
| 72 | registerStatusDatasetHandler("channels", bind(&FaceManager::listChannels, this, _1, _2, _3)); |
| 73 | registerStatusDatasetHandler("query", bind(&FaceManager::queryFaces, this, _1, _2, _3)); |
| 74 | |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 75 | m_postNotification = registerNotificationStream("events"); |
| 76 | m_faceAddConn = m_faceTable.afterAdd.connect(bind(&FaceManager::notifyAddFace, this, _1)); |
| 77 | m_faceRemoveConn = m_faceTable.beforeRemove.connect(bind(&FaceManager::notifyRemoveFace, this, _1)); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
| 81 | FaceManager::setConfigFile(ConfigFile& configFile) |
| 82 | { |
| 83 | configFile.addSectionHandler("face_system", bind(&FaceManager::processConfig, this, _1, _2, _3)); |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | FaceManager::createFace(const Name& topPrefix, const Interest& interest, |
| 88 | const ControlParameters& parameters, |
| 89 | const ndn::mgmt::CommandContinuation& done) |
| 90 | { |
| 91 | FaceUri uri; |
| 92 | if (!uri.parse(parameters.getUri())) { |
| 93 | NFD_LOG_TRACE("failed to parse URI"); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 94 | done(ControlResponse(400, "Malformed command")); |
| 95 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | if (!uri.isCanonical()) { |
| 99 | NFD_LOG_TRACE("received non-canonical URI"); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 100 | done(ControlResponse(400, "Non-canonical URI")); |
| 101 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 104 | auto factory = m_factories.find(uri.getScheme()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 105 | if (factory == m_factories.end()) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 106 | NFD_LOG_TRACE("received create request for unsupported protocol"); |
| 107 | done(ControlResponse(406, "Unsupported protocol")); |
| 108 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | try { |
| 112 | factory->second->createFace(uri, |
| 113 | parameters.getFacePersistency(), |
| 114 | bind(&FaceManager::afterCreateFaceSuccess, |
| 115 | this, parameters, _1, done), |
| 116 | bind(&FaceManager::afterCreateFaceFailure, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 117 | this, _1, _2, done)); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 118 | } |
| 119 | catch (const std::runtime_error& error) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 120 | NFD_LOG_ERROR("Face creation failed: " << error.what()); |
| 121 | done(ControlResponse(500, "Face creation failed due to internal error")); |
| 122 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 123 | } |
| 124 | catch (const std::logic_error& error) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 125 | NFD_LOG_ERROR("Face creation failed: " << error.what()); |
| 126 | done(ControlResponse(500, "Face creation failed due to internal error")); |
| 127 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 131 | /** |
| 132 | * \todo #3232 |
| 133 | * If the creation of this face would conflict with an existing face (e.g. same underlying |
| 134 | * protocol and remote address, or a NIC-associated permanent face), the command will fail |
| 135 | * with StatusCode 409. |
| 136 | */ |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 137 | void |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 138 | FaceManager::afterCreateFaceSuccess(const ControlParameters& parameters, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 139 | const shared_ptr<Face>& newFace, |
| 140 | const ndn::mgmt::CommandContinuation& done) |
| 141 | { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 142 | ControlParameters response; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 143 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 144 | m_faceTable.add(newFace); |
| 145 | |
| 146 | // Set ControlResponse parameters |
| 147 | response.setFaceId(newFace->getId()); |
| 148 | response.setFacePersistency(newFace->getPersistency()); |
| 149 | |
| 150 | done(ControlResponse(200, "OK").setBody(response.wireEncode())); |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | FaceManager::afterCreateFaceFailure(uint32_t status, |
| 155 | const std::string& reason, |
| 156 | const ndn::mgmt::CommandContinuation& done) |
| 157 | { |
| 158 | NFD_LOG_DEBUG("Face creation failed: " << reason); |
| 159 | |
| 160 | done(ControlResponse(status, reason)); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 164 | FaceManager::destroyFace(const Name& topPrefix, const Interest& interest, |
| 165 | const ControlParameters& parameters, |
| 166 | const ndn::mgmt::CommandContinuation& done) |
| 167 | { |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 168 | Face* face = m_faceTable.get(parameters.getFaceId()); |
| 169 | if (face != nullptr) { |
| 170 | face->close(); |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | done(ControlResponse(200, "OK").setBody(parameters.wireEncode())); |
| 174 | } |
| 175 | |
| 176 | void |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 177 | FaceManager::enableLocalControl(const Name& topPrefix, const Interest& interest, |
| 178 | const ControlParameters& parameters, |
| 179 | const ndn::mgmt::CommandContinuation& done) |
| 180 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 181 | Face* face = findFaceForLocalControl(interest, parameters, done); |
| 182 | if (!face) { |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 186 | // TODO#3226 redesign enable-local-control |
| 187 | // For now, enable-local-control will enable all local fields in GenericLinkService. |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 188 | auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService()); |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 189 | if (service == nullptr) { |
| 190 | return done(ControlResponse(503, "LinkService type not supported")); |
| 191 | } |
| 192 | |
| 193 | face::GenericLinkService::Options options = service->getOptions(); |
| 194 | options.allowLocalFields = true; |
| 195 | service->setOptions(options); |
| 196 | |
| 197 | return done(ControlResponse(200, "OK: enable all local fields on GenericLinkService") |
| 198 | .setBody(parameters.wireEncode())); |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | FaceManager::disableLocalControl(const Name& topPrefix, const Interest& interest, |
| 203 | const ControlParameters& parameters, |
| 204 | const ndn::mgmt::CommandContinuation& done) |
| 205 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 206 | Face* face = findFaceForLocalControl(interest, parameters, done); |
| 207 | if (!face) { |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 211 | // TODO#3226 redesign disable-local-control |
| 212 | // For now, disable-local-control will disable all local fields in GenericLinkService. |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 213 | auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService()); |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 214 | if (service == nullptr) { |
| 215 | return done(ControlResponse(503, "LinkService type not supported")); |
| 216 | } |
| 217 | |
| 218 | face::GenericLinkService::Options options = service->getOptions(); |
| 219 | options.allowLocalFields = false; |
| 220 | service->setOptions(options); |
| 221 | |
| 222 | return done(ControlResponse(200, "OK: disable all local fields on GenericLinkService") |
| 223 | .setBody(parameters.wireEncode())); |
| 224 | } |
| 225 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 226 | Face* |
| 227 | FaceManager::findFaceForLocalControl(const Interest& request, |
| 228 | const ControlParameters& parameters, |
| 229 | const ndn::mgmt::CommandContinuation& done) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 230 | { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 231 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>(); |
| 232 | // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field", |
| 233 | // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD |
| 234 | // and is initialized synchronously with IncomingFaceId field enabled. |
| 235 | BOOST_ASSERT(incomingFaceIdTag != nullptr); |
| 236 | |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 237 | Face* face = m_faceTable.get(*incomingFaceIdTag); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 238 | if (face == nullptr) { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 239 | NFD_LOG_DEBUG("FaceId " << *incomingFaceIdTag << " not found"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 240 | done(ControlResponse(410, "Face not found")); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 241 | return nullptr; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 244 | if (face->getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 245 | NFD_LOG_DEBUG("Cannot enable local control on non-local FaceId " << face->getId()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 246 | done(ControlResponse(412, "Face is non-local")); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 247 | return nullptr; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 250 | return face; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void |
| 254 | FaceManager::listFaces(const Name& topPrefix, const Interest& interest, |
| 255 | ndn::mgmt::StatusDatasetContext& context) |
| 256 | { |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 257 | auto now = time::steady_clock::now(); |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 258 | for (const Face& face : m_faceTable) { |
| 259 | ndn::nfd::FaceStatus status = collectFaceStatus(face, now); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 260 | context.append(status.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 261 | } |
| 262 | context.end(); |
| 263 | } |
| 264 | |
| 265 | void |
| 266 | FaceManager::listChannels(const Name& topPrefix, const Interest& interest, |
| 267 | ndn::mgmt::StatusDatasetContext& context) |
| 268 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 269 | std::set<const ProtocolFactory*> seenFactories; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 270 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 271 | for (const auto& kv : m_factories) { |
| 272 | const ProtocolFactory* factory = kv.second.get(); |
| 273 | bool inserted; |
| 274 | std::tie(std::ignore, inserted) = seenFactories.insert(factory); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 275 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 276 | if (inserted) { |
| 277 | for (const auto& channel : factory->getChannels()) { |
| 278 | ndn::nfd::ChannelStatus entry; |
| 279 | entry.setLocalUri(channel->getUri().toString()); |
| 280 | context.append(entry.wireEncode()); |
| 281 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | context.end(); |
| 286 | } |
| 287 | |
| 288 | void |
| 289 | FaceManager::queryFaces(const Name& topPrefix, const Interest& interest, |
| 290 | ndn::mgmt::StatusDatasetContext& context) |
| 291 | { |
| 292 | ndn::nfd::FaceQueryFilter faceFilter; |
| 293 | const Name& query = interest.getName(); |
| 294 | try { |
| 295 | faceFilter.wireDecode(query[-1].blockFromValue()); |
| 296 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 297 | catch (const tlv::Error& e) { |
| 298 | NFD_LOG_DEBUG("Malformed query filter: " << e.what()); |
| 299 | return context.reject(ControlResponse(400, "Malformed filter")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 302 | auto now = time::steady_clock::now(); |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 303 | for (const Face& face : m_faceTable) { |
| 304 | if (!matchFilter(faceFilter, face)) { |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 305 | continue; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 306 | } |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 307 | ndn::nfd::FaceStatus status = collectFaceStatus(face, now); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 308 | context.append(status.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 309 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 310 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 311 | context.end(); |
| 312 | } |
| 313 | |
| 314 | bool |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 315 | FaceManager::matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 316 | { |
| 317 | if (filter.hasFaceId() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 318 | filter.getFaceId() != static_cast<uint64_t>(face.getId())) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 319 | return false; |
| 320 | } |
| 321 | |
| 322 | if (filter.hasUriScheme() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 323 | filter.getUriScheme() != face.getRemoteUri().getScheme() && |
| 324 | filter.getUriScheme() != face.getLocalUri().getScheme()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 325 | return false; |
| 326 | } |
| 327 | |
| 328 | if (filter.hasRemoteUri() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 329 | filter.getRemoteUri() != face.getRemoteUri().toString()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | |
| 333 | if (filter.hasLocalUri() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 334 | filter.getLocalUri() != face.getLocalUri().toString()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 335 | return false; |
| 336 | } |
| 337 | |
| 338 | if (filter.hasFaceScope() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 339 | filter.getFaceScope() != face.getScope()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 340 | return false; |
| 341 | } |
| 342 | |
| 343 | if (filter.hasFacePersistency() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 344 | filter.getFacePersistency() != face.getPersistency()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 345 | return false; |
| 346 | } |
| 347 | |
| 348 | if (filter.hasLinkType() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 349 | filter.getLinkType() != face.getLinkType()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 350 | return false; |
| 351 | } |
| 352 | |
| 353 | return true; |
| 354 | } |
| 355 | |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 356 | ndn::nfd::FaceStatus |
| 357 | FaceManager::collectFaceStatus(const Face& face, const time::steady_clock::TimePoint& now) |
| 358 | { |
| 359 | ndn::nfd::FaceStatus status; |
| 360 | |
| 361 | collectFaceProperties(face, status); |
| 362 | |
| 363 | time::steady_clock::TimePoint expirationTime = face.getExpirationTime(); |
| 364 | if (expirationTime != time::steady_clock::TimePoint::max()) { |
| 365 | status.setExpirationPeriod(std::max(time::milliseconds(0), |
| 366 | time::duration_cast<time::milliseconds>(expirationTime - now))); |
| 367 | } |
| 368 | |
| 369 | const face::FaceCounters& counters = face.getCounters(); |
| 370 | status.setNInInterests(counters.nInInterests) |
| 371 | .setNOutInterests(counters.nOutInterests) |
| 372 | .setNInDatas(counters.nInData) |
| 373 | .setNOutDatas(counters.nOutData) |
| 374 | .setNInNacks(counters.nInNacks) |
| 375 | .setNOutNacks(counters.nOutNacks) |
| 376 | .setNInBytes(counters.nInBytes) |
| 377 | .setNOutBytes(counters.nOutBytes); |
| 378 | |
| 379 | return status; |
| 380 | } |
| 381 | |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 382 | template<typename FaceTraits> |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 383 | void |
| 384 | FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits) |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 385 | { |
| 386 | traits.setFaceId(face.getId()) |
| 387 | .setRemoteUri(face.getRemoteUri().toString()) |
| 388 | .setLocalUri(face.getLocalUri().toString()) |
| 389 | .setFaceScope(face.getScope()) |
| 390 | .setFacePersistency(face.getPersistency()) |
| 391 | .setLinkType(face.getLinkType()); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 395 | FaceManager::notifyAddFace(const Face& face) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 396 | { |
| 397 | ndn::nfd::FaceEventNotification notification; |
| 398 | notification.setKind(ndn::nfd::FACE_EVENT_CREATED); |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 399 | collectFaceProperties(face, notification); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 400 | |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 401 | m_postNotification(notification.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 405 | FaceManager::notifyRemoveFace(const Face& face) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 406 | { |
| 407 | ndn::nfd::FaceEventNotification notification; |
| 408 | notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED); |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 409 | collectFaceProperties(face, notification); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 410 | |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 411 | m_postNotification(notification.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void |
| 415 | FaceManager::processConfig(const ConfigSection& configSection, |
| 416 | bool isDryRun, |
| 417 | const std::string& filename) |
| 418 | { |
| 419 | bool hasSeenUnix = false; |
| 420 | bool hasSeenTcp = false; |
| 421 | bool hasSeenUdp = false; |
| 422 | bool hasSeenEther = false; |
| 423 | bool hasSeenWebSocket = false; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 424 | auto nicList = listNetworkInterfaces(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 425 | |
| 426 | for (const auto& item : configSection) { |
| 427 | if (item.first == "unix") { |
| 428 | if (hasSeenUnix) { |
| 429 | BOOST_THROW_EXCEPTION(Error("Duplicate \"unix\" section")); |
| 430 | } |
| 431 | hasSeenUnix = true; |
| 432 | |
| 433 | processSectionUnix(item.second, isDryRun); |
| 434 | } |
| 435 | else if (item.first == "tcp") { |
| 436 | if (hasSeenTcp) { |
| 437 | BOOST_THROW_EXCEPTION(Error("Duplicate \"tcp\" section")); |
| 438 | } |
| 439 | hasSeenTcp = true; |
| 440 | |
| 441 | processSectionTcp(item.second, isDryRun); |
| 442 | } |
| 443 | else if (item.first == "udp") { |
| 444 | if (hasSeenUdp) { |
| 445 | BOOST_THROW_EXCEPTION(Error("Duplicate \"udp\" section")); |
| 446 | } |
| 447 | hasSeenUdp = true; |
| 448 | |
| 449 | processSectionUdp(item.second, isDryRun, nicList); |
| 450 | } |
| 451 | else if (item.first == "ether") { |
| 452 | if (hasSeenEther) { |
| 453 | BOOST_THROW_EXCEPTION(Error("Duplicate \"ether\" section")); |
| 454 | } |
| 455 | hasSeenEther = true; |
| 456 | |
| 457 | processSectionEther(item.second, isDryRun, nicList); |
| 458 | } |
| 459 | else if (item.first == "websocket") { |
| 460 | if (hasSeenWebSocket) { |
| 461 | BOOST_THROW_EXCEPTION(Error("Duplicate \"websocket\" section")); |
| 462 | } |
| 463 | hasSeenWebSocket = true; |
| 464 | |
| 465 | processSectionWebSocket(item.second, isDryRun); |
| 466 | } |
| 467 | else { |
| 468 | BOOST_THROW_EXCEPTION(Error("Unrecognized option \"" + item.first + "\"")); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | void |
| 474 | FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun) |
| 475 | { |
| 476 | // ; the unix section contains settings of Unix stream faces and channels |
| 477 | // unix |
| 478 | // { |
| 479 | // path /var/run/nfd.sock ; Unix stream listener path |
| 480 | // } |
| 481 | |
| 482 | #if defined(HAVE_UNIX_SOCKETS) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 483 | std::string path = "/var/run/nfd.sock"; |
| 484 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 485 | for (const auto& i : configSection) { |
| 486 | if (i.first == "path") { |
| 487 | path = i.second.get_value<std::string>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 488 | } |
| 489 | else { |
| 490 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 491 | i.first + "\" in \"unix\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | if (!isDryRun) { |
| 496 | if (m_factories.count("unix") > 0) { |
| 497 | return; |
| 498 | } |
| 499 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 500 | auto factory = make_shared<UnixStreamFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 501 | m_factories.insert(std::make_pair("unix", factory)); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 502 | |
| 503 | auto channel = factory->createChannel(path); |
| 504 | channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 505 | } |
| 506 | #else |
| 507 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, " |
| 508 | "cannot process \"unix\" section")); |
| 509 | #endif // HAVE_UNIX_SOCKETS |
| 510 | } |
| 511 | |
| 512 | void |
| 513 | FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun) |
| 514 | { |
| 515 | // ; the tcp section contains settings of TCP faces and channels |
| 516 | // tcp |
| 517 | // { |
| 518 | // listen yes ; set to 'no' to disable TCP listener, default 'yes' |
| 519 | // port 6363 ; TCP listener port number |
| 520 | // } |
| 521 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 522 | uint16_t port = 6363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 523 | bool needToListen = true; |
| 524 | bool enableV4 = true; |
| 525 | bool enableV6 = true; |
| 526 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 527 | for (const auto& i : configSection) { |
| 528 | if (i.first == "port") { |
| 529 | port = ConfigFile::parseNumber<uint16_t>(i, "tcp"); |
| 530 | NFD_LOG_TRACE("TCP port set to " << port); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 531 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 532 | else if (i.first == "listen") { |
| 533 | needToListen = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 534 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 535 | else if (i.first == "enable_v4") { |
| 536 | enableV4 = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 537 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 538 | else if (i.first == "enable_v6") { |
| 539 | enableV6 = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 540 | } |
| 541 | else { |
| 542 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 543 | i.first + "\" in \"tcp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
| 547 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 548 | BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 TCP channels have been disabled." |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 549 | " Remove \"tcp\" section to disable TCP channels or" |
| 550 | " re-enable at least one channel type.")); |
| 551 | } |
| 552 | |
| 553 | if (!isDryRun) { |
| 554 | if (m_factories.count("tcp") > 0) { |
| 555 | return; |
| 556 | } |
| 557 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 558 | auto factory = make_shared<TcpFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 559 | m_factories.insert(std::make_pair("tcp", factory)); |
| 560 | |
| 561 | if (enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 562 | tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port); |
| 563 | shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 564 | if (needToListen) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 565 | v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | m_factories.insert(std::make_pair("tcp4", factory)); |
| 569 | } |
| 570 | |
| 571 | if (enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 572 | tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port); |
| 573 | shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 574 | if (needToListen) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 575 | v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | m_factories.insert(std::make_pair("tcp6", factory)); |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 584 | FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 585 | const std::vector<NetworkInterfaceInfo>& nicList) |
| 586 | { |
| 587 | // ; the udp section contains settings of UDP faces and channels |
| 588 | // udp |
| 589 | // { |
| 590 | // port 6363 ; UDP unicast port number |
| 591 | // idle_timeout 600 ; idle time (seconds) before closing a UDP unicast face |
| 592 | // keep_alive_interval 25 ; interval (seconds) between keep-alive refreshes |
| 593 | |
| 594 | // ; NFD creates one UDP multicast face per NIC |
| 595 | // mcast yes ; set to 'no' to disable UDP multicast, default 'yes' |
| 596 | // mcast_port 56363 ; UDP multicast port number |
| 597 | // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only) |
| 598 | // } |
| 599 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 600 | uint16_t port = 6363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 601 | bool enableV4 = true; |
| 602 | bool enableV6 = true; |
| 603 | size_t timeout = 600; |
| 604 | size_t keepAliveInterval = 25; |
| 605 | bool useMcast = true; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 606 | auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170"); |
| 607 | uint16_t mcastPort = 56363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 608 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 609 | for (const auto& i : configSection) { |
| 610 | if (i.first == "port") { |
| 611 | port = ConfigFile::parseNumber<uint16_t>(i, "udp"); |
| 612 | NFD_LOG_TRACE("UDP unicast port set to " << port); |
| 613 | } |
| 614 | else if (i.first == "enable_v4") { |
| 615 | enableV4 = ConfigFile::parseYesNo(i, "udp"); |
| 616 | } |
| 617 | else if (i.first == "enable_v6") { |
| 618 | enableV6 = ConfigFile::parseYesNo(i, "udp"); |
| 619 | } |
| 620 | else if (i.first == "idle_timeout") { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 621 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 622 | timeout = i.second.get_value<size_t>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 623 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 624 | catch (const boost::property_tree::ptree_bad_data&) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 625 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 626 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 629 | else if (i.first == "keep_alive_interval") { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 630 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 631 | keepAliveInterval = i.second.get_value<size_t>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 632 | /// \todo Make use of keepAliveInterval |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 633 | (void)(keepAliveInterval); |
| 634 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 635 | catch (const boost::property_tree::ptree_bad_data&) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 636 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 637 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 638 | } |
| 639 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 640 | else if (i.first == "mcast") { |
| 641 | useMcast = ConfigFile::parseYesNo(i, "udp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 642 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 643 | else if (i.first == "mcast_port") { |
| 644 | mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp"); |
| 645 | NFD_LOG_TRACE("UDP multicast port set to " << mcastPort); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 646 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 647 | else if (i.first == "mcast_group") { |
| 648 | boost::system::error_code ec; |
| 649 | mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec); |
| 650 | if (ec) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 651 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 652 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 653 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 654 | NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 655 | } |
| 656 | else { |
| 657 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 658 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
| 662 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 663 | BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 UDP channels have been disabled." |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 664 | " Remove \"udp\" section to disable UDP channels or" |
| 665 | " re-enable at least one channel type.")); |
| 666 | } |
| 667 | else if (useMcast && !enableV4) { |
| 668 | BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 multicast requested, but IPv4 channels" |
| 669 | " have been disabled (conflicting configuration options set)")); |
| 670 | } |
| 671 | |
| 672 | if (!isDryRun) { |
| 673 | shared_ptr<UdpFactory> factory; |
| 674 | bool isReload = false; |
| 675 | if (m_factories.count("udp") > 0) { |
| 676 | isReload = true; |
| 677 | factory = static_pointer_cast<UdpFactory>(m_factories["udp"]); |
| 678 | } |
| 679 | else { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 680 | factory = make_shared<UdpFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 681 | m_factories.insert(std::make_pair("udp", factory)); |
| 682 | } |
| 683 | |
| 684 | if (!isReload && enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 685 | udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port); |
| 686 | shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout)); |
| 687 | v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 688 | |
| 689 | m_factories.insert(std::make_pair("udp4", factory)); |
| 690 | } |
| 691 | |
| 692 | if (!isReload && enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 693 | udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port); |
| 694 | shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout)); |
| 695 | v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 696 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 697 | m_factories.insert(std::make_pair("udp6", factory)); |
| 698 | } |
| 699 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 700 | std::set<shared_ptr<Face>> multicastFacesToRemove; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 701 | for (const auto& i : factory->getMulticastFaces()) { |
| 702 | multicastFacesToRemove.insert(i.second); |
| 703 | } |
| 704 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 705 | if (useMcast && enableV4) { |
| 706 | std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces; |
| 707 | for (const auto& nic : nicList) { |
| 708 | if (nic.isUp() && nic.isMulticastCapable() && !nic.ipv4Addresses.empty()) { |
| 709 | ipv4MulticastInterfaces.push_back(nic); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | bool isNicNameNecessary = false; |
| 714 | #if defined(__linux__) |
| 715 | if (ipv4MulticastInterfaces.size() > 1) { |
| 716 | // On Linux if we have more than one MulticastUdpFace |
| 717 | // we need to specify the name of the interface |
| 718 | isNicNameNecessary = true; |
| 719 | } |
| 720 | #endif |
| 721 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 722 | udp::Endpoint mcastEndpoint(mcastGroup, mcastPort); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 723 | for (const auto& nic : ipv4MulticastInterfaces) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 724 | udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort); |
| 725 | auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint, |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 726 | isNicNameNecessary ? nic.name : ""); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 727 | m_faceTable.add(newFace); |
| 728 | multicastFacesToRemove.erase(newFace); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 729 | } |
| 730 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 731 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 732 | for (const auto& face : multicastFacesToRemove) { |
| 733 | face->close(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 739 | FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 740 | const std::vector<NetworkInterfaceInfo>& nicList) |
| 741 | { |
| 742 | // ; the ether section contains settings of Ethernet faces and channels |
| 743 | // ether |
| 744 | // { |
| 745 | // ; NFD creates one Ethernet multicast face per NIC |
| 746 | // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes' |
| 747 | // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group |
| 748 | // } |
| 749 | |
| 750 | #if defined(HAVE_LIBPCAP) |
| 751 | bool useMcast = true; |
| 752 | ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress()); |
| 753 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 754 | for (const auto& i : configSection) { |
| 755 | if (i.first == "mcast") { |
| 756 | useMcast = ConfigFile::parseYesNo(i, "ether"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 757 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 758 | else if (i.first == "mcast_group") { |
| 759 | mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 760 | if (mcastGroup.isNull()) { |
| 761 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 762 | i.first + "\" in \"ether\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 763 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 764 | NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 765 | } |
| 766 | else { |
| 767 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 768 | i.first + "\" in \"ether\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
| 772 | if (!isDryRun) { |
| 773 | shared_ptr<EthernetFactory> factory; |
| 774 | if (m_factories.count("ether") > 0) { |
| 775 | factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]); |
| 776 | } |
| 777 | else { |
| 778 | factory = make_shared<EthernetFactory>(); |
| 779 | m_factories.insert(std::make_pair("ether", factory)); |
| 780 | } |
| 781 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 782 | std::set<shared_ptr<Face>> multicastFacesToRemove; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 783 | for (const auto& i : factory->getMulticastFaces()) { |
| 784 | multicastFacesToRemove.insert(i.second); |
| 785 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 786 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 787 | if (useMcast) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 788 | for (const auto& nic : nicList) { |
| 789 | if (nic.isUp() && nic.isMulticastCapable()) { |
| 790 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 791 | auto newFace = factory->createMulticastFace(nic, mcastGroup); |
| 792 | m_faceTable.add(newFace); |
| 793 | multicastFacesToRemove.erase(newFace); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 794 | } |
| 795 | catch (const EthernetFactory::Error& factoryError) { |
| 796 | NFD_LOG_ERROR(factoryError.what() << ", continuing"); |
| 797 | } |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 798 | catch (const face::EthernetTransport::Error& faceError) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 799 | NFD_LOG_ERROR(faceError.what() << ", continuing"); |
| 800 | } |
| 801 | } |
| 802 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 803 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 804 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 805 | for (const auto& face : multicastFacesToRemove) { |
| 806 | face->close(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | #else |
| 810 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section")); |
| 811 | #endif // HAVE_LIBPCAP |
| 812 | } |
| 813 | |
| 814 | void |
| 815 | FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun) |
| 816 | { |
| 817 | // ; the websocket section contains settings of WebSocket faces and channels |
| 818 | // websocket |
| 819 | // { |
| 820 | // listen yes ; set to 'no' to disable WebSocket listener, default 'yes' |
| 821 | // port 9696 ; WebSocket listener port number |
| 822 | // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes' |
| 823 | // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes' |
| 824 | // } |
| 825 | |
| 826 | #if defined(HAVE_WEBSOCKET) |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 827 | uint16_t port = 9696; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 828 | bool needToListen = true; |
| 829 | bool enableV4 = true; |
| 830 | bool enableV6 = true; |
| 831 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 832 | for (const auto& i : configSection) { |
| 833 | if (i.first == "port") { |
| 834 | port = ConfigFile::parseNumber<uint16_t>(i, "websocket"); |
| 835 | NFD_LOG_TRACE("WebSocket port set to " << port); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 836 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 837 | else if (i.first == "listen") { |
| 838 | needToListen = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 839 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 840 | else if (i.first == "enable_v4") { |
| 841 | enableV4 = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 842 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 843 | else if (i.first == "enable_v6") { |
| 844 | enableV6 = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 845 | } |
| 846 | else { |
| 847 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 848 | i.first + "\" in \"websocket\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
| 852 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 853 | BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 WebSocket channels have been disabled." |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 854 | " Remove \"websocket\" section to disable WebSocket channels or" |
| 855 | " re-enable at least one channel type.")); |
| 856 | } |
| 857 | |
| 858 | if (!enableV4 && enableV6) { |
| 859 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel.")); |
| 860 | } |
| 861 | |
| 862 | if (!isDryRun) { |
| 863 | if (m_factories.count("websocket") > 0) { |
| 864 | return; |
| 865 | } |
| 866 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 867 | auto factory = make_shared<WebSocketFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 868 | m_factories.insert(std::make_pair("websocket", factory)); |
| 869 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 870 | shared_ptr<WebSocketChannel> channel; |
| 871 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 872 | if (enableV6 && enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 873 | websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port); |
| 874 | channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 875 | |
| 876 | m_factories.insert(std::make_pair("websocket46", factory)); |
| 877 | } |
| 878 | else if (enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 879 | websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port); |
| 880 | channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 881 | |
| 882 | m_factories.insert(std::make_pair("websocket4", factory)); |
| 883 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 884 | |
| 885 | if (channel && needToListen) { |
| 886 | channel->listen(bind(&FaceTable::add, &m_faceTable, _1)); |
| 887 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 888 | } |
| 889 | #else |
| 890 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, " |
| 891 | "cannot process \"websocket\" section")); |
| 892 | #endif // HAVE_WEBSOCKET |
| 893 | } |
| 894 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 895 | } // namespace nfd |