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