Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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" |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 27 | #include "face/generic-link-service.hpp" |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 28 | #include "face/protocol-factory.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 29 | #include "fw/face-table.hpp" |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 30 | |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/lp/tags.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/mgmt/nfd/channel-status.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 | namespace nfd { |
| 35 | |
| 36 | NFD_LOG_INIT("FaceManager"); |
| 37 | |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 38 | FaceManager::FaceManager(FaceSystem& faceSystem, |
| 39 | Dispatcher& dispatcher, CommandAuthenticator& authenticator) |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 40 | : NfdManagerBase(dispatcher, authenticator, "faces") |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 41 | , m_faceSystem(faceSystem) |
| 42 | , m_faceTable(faceSystem.getFaceTable()) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 43 | { |
| 44 | registerCommandHandler<ndn::nfd::FaceCreateCommand>("create", |
| 45 | bind(&FaceManager::createFace, this, _2, _3, _4, _5)); |
| 46 | |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 47 | registerCommandHandler<ndn::nfd::FaceUpdateCommand>("update", |
| 48 | bind(&FaceManager::updateFace, this, _2, _3, _4, _5)); |
| 49 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 50 | registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy", |
| 51 | bind(&FaceManager::destroyFace, this, _2, _3, _4, _5)); |
| 52 | |
| 53 | registerCommandHandler<ndn::nfd::FaceEnableLocalControlCommand>("enable-local-control", |
| 54 | bind(&FaceManager::enableLocalControl, this, _2, _3, _4, _5)); |
| 55 | |
| 56 | registerCommandHandler<ndn::nfd::FaceDisableLocalControlCommand>("disable-local-control", |
| 57 | bind(&FaceManager::disableLocalControl, this, _2, _3, _4, _5)); |
| 58 | |
| 59 | registerStatusDatasetHandler("list", bind(&FaceManager::listFaces, this, _1, _2, _3)); |
| 60 | registerStatusDatasetHandler("channels", bind(&FaceManager::listChannels, this, _1, _2, _3)); |
| 61 | registerStatusDatasetHandler("query", bind(&FaceManager::queryFaces, this, _1, _2, _3)); |
| 62 | |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 63 | m_postNotification = registerNotificationStream("events"); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 64 | m_faceAddConn = m_faceTable.afterAdd.connect([this] (const Face& face) { |
| 65 | connectFaceStateChangeSignal(face); |
| 66 | notifyFaceEvent(face, ndn::nfd::FACE_EVENT_CREATED); |
| 67 | }); |
| 68 | m_faceRemoveConn = m_faceTable.beforeRemove.connect([this] (const Face& face) { |
| 69 | notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DESTROYED); |
| 70 | }); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
| 74 | FaceManager::setConfigFile(ConfigFile& configFile) |
| 75 | { |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 76 | m_faceSystem.setConfigFile(configFile); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
| 80 | FaceManager::createFace(const Name& topPrefix, const Interest& interest, |
| 81 | const ControlParameters& parameters, |
| 82 | const ndn::mgmt::CommandContinuation& done) |
| 83 | { |
| 84 | FaceUri uri; |
| 85 | if (!uri.parse(parameters.getUri())) { |
| 86 | NFD_LOG_TRACE("failed to parse URI"); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 87 | done(ControlResponse(400, "Malformed command")); |
| 88 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (!uri.isCanonical()) { |
| 92 | NFD_LOG_TRACE("received non-canonical URI"); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 93 | done(ControlResponse(400, "Non-canonical URI")); |
| 94 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 97 | ProtocolFactory* factory = m_faceSystem.getFactoryByScheme(uri.getScheme()); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 98 | if (factory == nullptr) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 99 | NFD_LOG_TRACE("received create request for unsupported protocol"); |
| 100 | done(ControlResponse(406, "Unsupported protocol")); |
| 101 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | try { |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 105 | factory->createFace(uri, |
| 106 | parameters.getFacePersistency(), |
| 107 | parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 108 | parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED), |
| 109 | bind(&FaceManager::afterCreateFaceSuccess, this, parameters, _1, done), |
| 110 | bind(&FaceManager::afterCreateFaceFailure, this, _1, _2, done)); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 111 | } |
| 112 | catch (const std::runtime_error& error) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 113 | NFD_LOG_ERROR("Face creation failed: " << error.what()); |
| 114 | done(ControlResponse(500, "Face creation failed due to internal error")); |
| 115 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 116 | } |
| 117 | catch (const std::logic_error& error) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 118 | NFD_LOG_ERROR("Face creation failed: " << error.what()); |
| 119 | done(ControlResponse(500, "Face creation failed due to internal error")); |
| 120 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 124 | /** |
| 125 | * \todo #3232 |
| 126 | * If the creation of this face would conflict with an existing face (e.g. same underlying |
| 127 | * protocol and remote address, or a NIC-associated permanent face), the command will fail |
| 128 | * with StatusCode 409. |
| 129 | */ |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 130 | void |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 131 | FaceManager::afterCreateFaceSuccess(const ControlParameters& parameters, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 132 | const shared_ptr<Face>& face, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 133 | const ndn::mgmt::CommandContinuation& done) |
| 134 | { |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 135 | // TODO: Re-enable check in #3232 |
| 136 | //if (face->getId() != face::INVALID_FACEID) { |
| 137 | //// Face already exists |
| 138 | //ControlParameters response; |
| 139 | //response.setFaceId(face->getId()); |
| 140 | //response.setUri(face->getRemoteUri().toString()); |
| 141 | //response.setFacePersistency(face->getPersistency()); |
| 142 | // |
| 143 | //auto linkService = dynamic_cast<face::GenericLinkService*>(face->getLinkService()); |
| 144 | //BOOST_ASSERT(linkService != nullptr); |
| 145 | //auto options = linkService->getOptions(); |
| 146 | //response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false); |
| 147 | // |
| 148 | // NFD_LOG_TRACE("Attempted to create duplicate face of " << face->getId()); |
| 149 | // done(ControlResponse(409, "Face with remote URI already exists").setBody(response.wireEncode())); |
| 150 | //} |
| 151 | //else { |
| 152 | // If scope non-local and flags set to enable local fields, request shouldn't |
| 153 | // have made it this far |
| 154 | BOOST_ASSERT(face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL || |
| 155 | !parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) || |
| 156 | (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 157 | !parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED))); |
| 158 | |
| 159 | m_faceTable.add(face); |
| 160 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 161 | ControlParameters response; |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 162 | response.setFaceId(face->getId()); |
| 163 | response.setFacePersistency(face->getPersistency()); |
| 164 | response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, |
| 165 | parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ? |
| 166 | parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) : false, |
| 167 | false); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 168 | |
| 169 | done(ControlResponse(200, "OK").setBody(response.wireEncode())); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 170 | //} |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void |
| 174 | FaceManager::afterCreateFaceFailure(uint32_t status, |
| 175 | const std::string& reason, |
| 176 | const ndn::mgmt::CommandContinuation& done) |
| 177 | { |
| 178 | NFD_LOG_DEBUG("Face creation failed: " << reason); |
| 179 | |
| 180 | done(ControlResponse(status, reason)); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 184 | FaceManager::updateFace(const Name& topPrefix, const Interest& interest, |
| 185 | const ControlParameters& parameters, |
| 186 | const ndn::mgmt::CommandContinuation& done) |
| 187 | { |
| 188 | FaceId faceId = parameters.getFaceId(); |
| 189 | if (faceId == 0) { |
| 190 | // Self-updating |
| 191 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>(); |
| 192 | if (incomingFaceIdTag == nullptr) { |
| 193 | NFD_LOG_TRACE("unable to determine face for self-update"); |
| 194 | done(ControlResponse(404, "No FaceId specified and IncomingFaceId not available")); |
| 195 | return; |
| 196 | } |
| 197 | faceId = *incomingFaceIdTag; |
| 198 | } |
| 199 | |
| 200 | Face* face = m_faceTable.get(faceId); |
| 201 | |
| 202 | if (face == nullptr) { |
| 203 | NFD_LOG_TRACE("invalid face specified"); |
| 204 | done(ControlResponse(404, "Specified face does not exist")); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | // Verify validity of requested changes |
| 209 | ControlParameters response; |
| 210 | bool areParamsValid = true; |
| 211 | |
| 212 | if (parameters.hasFacePersistency()) { |
| 213 | // TODO #3232: Add FacePersistency updating |
| 214 | NFD_LOG_TRACE("received unsupported face persistency change"); |
| 215 | areParamsValid = false; |
| 216 | response.setFacePersistency(parameters.getFacePersistency()); |
| 217 | } |
| 218 | |
| 219 | if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 220 | parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 221 | face->getScope() != ndn::nfd::FACE_SCOPE_LOCAL) { |
| 222 | NFD_LOG_TRACE("received request to enable local fields on non-local face"); |
| 223 | areParamsValid = false; |
| 224 | response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, |
| 225 | parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)); |
| 226 | } |
| 227 | |
| 228 | if (!areParamsValid) { |
| 229 | done(ControlResponse(409, "Invalid properties specified").setBody(response.wireEncode())); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | // All specified properties are valid, so make changes |
| 234 | |
| 235 | // TODO #3232: Add FacePersistency updating |
| 236 | |
| 237 | setLinkServiceOptions(*face, parameters, response); |
| 238 | |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 239 | // Set ControlResponse fields |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 240 | response.setFaceId(faceId); |
| 241 | response.setFacePersistency(face->getPersistency()); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 242 | response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, |
| 243 | parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ? |
| 244 | parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) : false, |
| 245 | false); |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 246 | |
| 247 | done(ControlResponse(200, "OK").setBody(response.wireEncode())); |
| 248 | } |
| 249 | |
| 250 | void |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 251 | FaceManager::destroyFace(const Name& topPrefix, const Interest& interest, |
| 252 | const ControlParameters& parameters, |
| 253 | const ndn::mgmt::CommandContinuation& done) |
| 254 | { |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 255 | Face* face = m_faceTable.get(parameters.getFaceId()); |
| 256 | if (face != nullptr) { |
| 257 | face->close(); |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | done(ControlResponse(200, "OK").setBody(parameters.wireEncode())); |
| 261 | } |
| 262 | |
| 263 | void |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 264 | FaceManager::enableLocalControl(const Name& topPrefix, const Interest& interest, |
| 265 | const ControlParameters& parameters, |
| 266 | const ndn::mgmt::CommandContinuation& done) |
| 267 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 268 | Face* face = findFaceForLocalControl(interest, parameters, done); |
| 269 | if (!face) { |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 270 | return; |
| 271 | } |
| 272 | |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 273 | // enable-local-control will enable all local fields in GenericLinkService |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 274 | auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService()); |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 275 | if (service == nullptr) { |
| 276 | return done(ControlResponse(503, "LinkService type not supported")); |
| 277 | } |
| 278 | |
| 279 | face::GenericLinkService::Options options = service->getOptions(); |
| 280 | options.allowLocalFields = true; |
| 281 | service->setOptions(options); |
| 282 | |
| 283 | return done(ControlResponse(200, "OK: enable all local fields on GenericLinkService") |
| 284 | .setBody(parameters.wireEncode())); |
| 285 | } |
| 286 | |
| 287 | void |
| 288 | FaceManager::disableLocalControl(const Name& topPrefix, const Interest& interest, |
| 289 | const ControlParameters& parameters, |
| 290 | const ndn::mgmt::CommandContinuation& done) |
| 291 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 292 | Face* face = findFaceForLocalControl(interest, parameters, done); |
| 293 | if (!face) { |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 297 | // disable-local-control will disable all local fields in GenericLinkService |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 298 | auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService()); |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 299 | if (service == nullptr) { |
| 300 | return done(ControlResponse(503, "LinkService type not supported")); |
| 301 | } |
| 302 | |
| 303 | face::GenericLinkService::Options options = service->getOptions(); |
| 304 | options.allowLocalFields = false; |
| 305 | service->setOptions(options); |
| 306 | |
| 307 | return done(ControlResponse(200, "OK: disable all local fields on GenericLinkService") |
| 308 | .setBody(parameters.wireEncode())); |
| 309 | } |
| 310 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 311 | Face* |
| 312 | FaceManager::findFaceForLocalControl(const Interest& request, |
| 313 | const ControlParameters& parameters, |
| 314 | const ndn::mgmt::CommandContinuation& done) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 315 | { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 316 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>(); |
| 317 | // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field", |
| 318 | // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD |
| 319 | // and is initialized synchronously with IncomingFaceId field enabled. |
| 320 | BOOST_ASSERT(incomingFaceIdTag != nullptr); |
| 321 | |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 322 | Face* face = m_faceTable.get(*incomingFaceIdTag); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 323 | if (face == nullptr) { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 324 | NFD_LOG_DEBUG("FaceId " << *incomingFaceIdTag << " not found"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 325 | done(ControlResponse(410, "Face not found")); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 326 | return nullptr; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 329 | if (face->getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 330 | 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] | 331 | done(ControlResponse(412, "Face is non-local")); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 332 | return nullptr; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 335 | return face; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 339 | FaceManager::setLinkServiceOptions(Face& face, |
| 340 | const ControlParameters& parameters, |
| 341 | ControlParameters& response) |
| 342 | { |
| 343 | auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService()); |
| 344 | BOOST_ASSERT(linkService != nullptr); |
| 345 | |
| 346 | auto options = linkService->getOptions(); |
| 347 | if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 348 | face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) { |
| 349 | options.allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED); |
| 350 | } |
| 351 | linkService->setOptions(options); |
| 352 | |
| 353 | // Set Flags for ControlResponse |
| 354 | response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false); |
| 355 | } |
| 356 | |
| 357 | void |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 358 | FaceManager::listFaces(const Name& topPrefix, const Interest& interest, |
| 359 | ndn::mgmt::StatusDatasetContext& context) |
| 360 | { |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 361 | auto now = time::steady_clock::now(); |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 362 | for (const Face& face : m_faceTable) { |
| 363 | ndn::nfd::FaceStatus status = collectFaceStatus(face, now); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 364 | context.append(status.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 365 | } |
| 366 | context.end(); |
| 367 | } |
| 368 | |
| 369 | void |
| 370 | FaceManager::listChannels(const Name& topPrefix, const Interest& interest, |
| 371 | ndn::mgmt::StatusDatasetContext& context) |
| 372 | { |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 373 | std::set<const ProtocolFactory*> factories = m_faceSystem.listProtocolFactories(); |
| 374 | for (const ProtocolFactory* factory : factories) { |
| 375 | for (const auto& channel : factory->getChannels()) { |
| 376 | ndn::nfd::ChannelStatus entry; |
| 377 | entry.setLocalUri(channel->getUri().toString()); |
| 378 | context.append(entry.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 381 | context.end(); |
| 382 | } |
| 383 | |
| 384 | void |
| 385 | FaceManager::queryFaces(const Name& topPrefix, const Interest& interest, |
| 386 | ndn::mgmt::StatusDatasetContext& context) |
| 387 | { |
| 388 | ndn::nfd::FaceQueryFilter faceFilter; |
| 389 | const Name& query = interest.getName(); |
| 390 | try { |
| 391 | faceFilter.wireDecode(query[-1].blockFromValue()); |
| 392 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 393 | catch (const tlv::Error& e) { |
| 394 | NFD_LOG_DEBUG("Malformed query filter: " << e.what()); |
| 395 | return context.reject(ControlResponse(400, "Malformed filter")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 398 | auto now = time::steady_clock::now(); |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 399 | for (const Face& face : m_faceTable) { |
| 400 | if (!matchFilter(faceFilter, face)) { |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 401 | continue; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 402 | } |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 403 | ndn::nfd::FaceStatus status = collectFaceStatus(face, now); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 404 | context.append(status.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 405 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 406 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 407 | context.end(); |
| 408 | } |
| 409 | |
| 410 | bool |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 411 | FaceManager::matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 412 | { |
| 413 | if (filter.hasFaceId() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 414 | filter.getFaceId() != static_cast<uint64_t>(face.getId())) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 415 | return false; |
| 416 | } |
| 417 | |
| 418 | if (filter.hasUriScheme() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 419 | filter.getUriScheme() != face.getRemoteUri().getScheme() && |
| 420 | filter.getUriScheme() != face.getLocalUri().getScheme()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 421 | return false; |
| 422 | } |
| 423 | |
| 424 | if (filter.hasRemoteUri() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 425 | filter.getRemoteUri() != face.getRemoteUri().toString()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 426 | return false; |
| 427 | } |
| 428 | |
| 429 | if (filter.hasLocalUri() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 430 | filter.getLocalUri() != face.getLocalUri().toString()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 431 | return false; |
| 432 | } |
| 433 | |
| 434 | if (filter.hasFaceScope() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 435 | filter.getFaceScope() != face.getScope()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 436 | return false; |
| 437 | } |
| 438 | |
| 439 | if (filter.hasFacePersistency() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 440 | filter.getFacePersistency() != face.getPersistency()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 441 | return false; |
| 442 | } |
| 443 | |
| 444 | if (filter.hasLinkType() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 445 | filter.getLinkType() != face.getLinkType()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 446 | return false; |
| 447 | } |
| 448 | |
| 449 | return true; |
| 450 | } |
| 451 | |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 452 | ndn::nfd::FaceStatus |
| 453 | FaceManager::collectFaceStatus(const Face& face, const time::steady_clock::TimePoint& now) |
| 454 | { |
| 455 | ndn::nfd::FaceStatus status; |
| 456 | |
| 457 | collectFaceProperties(face, status); |
| 458 | |
| 459 | time::steady_clock::TimePoint expirationTime = face.getExpirationTime(); |
| 460 | if (expirationTime != time::steady_clock::TimePoint::max()) { |
| 461 | status.setExpirationPeriod(std::max(time::milliseconds(0), |
| 462 | time::duration_cast<time::milliseconds>(expirationTime - now))); |
| 463 | } |
| 464 | |
| 465 | const face::FaceCounters& counters = face.getCounters(); |
| 466 | status.setNInInterests(counters.nInInterests) |
| 467 | .setNOutInterests(counters.nOutInterests) |
| 468 | .setNInDatas(counters.nInData) |
| 469 | .setNOutDatas(counters.nOutData) |
| 470 | .setNInNacks(counters.nInNacks) |
| 471 | .setNOutNacks(counters.nOutNacks) |
| 472 | .setNInBytes(counters.nInBytes) |
| 473 | .setNOutBytes(counters.nOutBytes); |
| 474 | |
| 475 | return status; |
| 476 | } |
| 477 | |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 478 | template<typename FaceTraits> |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 479 | void |
| 480 | FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits) |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 481 | { |
| 482 | traits.setFaceId(face.getId()) |
| 483 | .setRemoteUri(face.getRemoteUri().toString()) |
| 484 | .setLocalUri(face.getLocalUri().toString()) |
| 485 | .setFaceScope(face.getScope()) |
| 486 | .setFacePersistency(face.getPersistency()) |
| 487 | .setLinkType(face.getLinkType()); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 488 | |
| 489 | // Set Flag bits |
| 490 | auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService()); |
| 491 | if (linkService != nullptr) { |
| 492 | auto linkServiceOptions = linkService->getOptions(); |
| 493 | traits.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, linkServiceOptions.allowLocalFields); |
| 494 | } |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 498 | FaceManager::notifyFaceEvent(const Face& face, ndn::nfd::FaceEventKind kind) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 499 | { |
| 500 | ndn::nfd::FaceEventNotification notification; |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 501 | notification.setKind(kind); |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 502 | collectFaceProperties(face, notification); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 503 | |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 504 | m_postNotification(notification.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | void |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 508 | FaceManager::connectFaceStateChangeSignal(const Face& face) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 509 | { |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 510 | FaceId faceId = face.getId(); |
| 511 | m_faceStateChangeConn[faceId] = face.afterStateChange.connect( |
| 512 | [this, faceId] (face::FaceState oldState, face::FaceState newState) { |
| 513 | const Face& face = *m_faceTable.get(faceId); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 514 | |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 515 | if (newState == face::FaceState::UP) { |
| 516 | notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP); |
| 517 | } |
| 518 | else if (newState == face::FaceState::DOWN) { |
| 519 | notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN); |
| 520 | } |
| 521 | else if (newState == face::FaceState::CLOSED) { |
| 522 | m_faceStateChangeConn.erase(faceId); |
| 523 | } |
| 524 | }); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 527 | } // namespace nfd |