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