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> |
| 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 | |
Eric Newberry | c05918c | 2016-09-29 10:38:13 -0700 | [diff] [blame] | 496 | // Set Flag bits |
| 497 | auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService()); |
| 498 | if (linkService != nullptr) { |
| 499 | auto linkServiceOptions = linkService->getOptions(); |
| 500 | status.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, linkServiceOptions.allowLocalFields); |
| 501 | } |
| 502 | |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 503 | return status; |
| 504 | } |
| 505 | |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 506 | template<typename FaceTraits> |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 507 | void |
| 508 | FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits) |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 509 | { |
| 510 | traits.setFaceId(face.getId()) |
| 511 | .setRemoteUri(face.getRemoteUri().toString()) |
| 512 | .setLocalUri(face.getLocalUri().toString()) |
| 513 | .setFaceScope(face.getScope()) |
| 514 | .setFacePersistency(face.getPersistency()) |
| 515 | .setLinkType(face.getLinkType()); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | void |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 519 | FaceManager::notifyAddFace(const Face& face) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 520 | { |
| 521 | ndn::nfd::FaceEventNotification notification; |
| 522 | notification.setKind(ndn::nfd::FACE_EVENT_CREATED); |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 523 | collectFaceProperties(face, notification); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 524 | |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 525 | m_postNotification(notification.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | void |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 529 | FaceManager::notifyRemoveFace(const Face& face) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 530 | { |
| 531 | ndn::nfd::FaceEventNotification notification; |
| 532 | notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED); |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 533 | collectFaceProperties(face, notification); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 534 | |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 535 | m_postNotification(notification.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | void |
| 539 | FaceManager::processConfig(const ConfigSection& configSection, |
| 540 | bool isDryRun, |
| 541 | const std::string& filename) |
| 542 | { |
| 543 | bool hasSeenUnix = false; |
| 544 | bool hasSeenTcp = false; |
| 545 | bool hasSeenUdp = false; |
| 546 | bool hasSeenEther = false; |
| 547 | bool hasSeenWebSocket = false; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 548 | auto nicList = listNetworkInterfaces(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 549 | |
| 550 | for (const auto& item : configSection) { |
| 551 | if (item.first == "unix") { |
| 552 | if (hasSeenUnix) { |
| 553 | BOOST_THROW_EXCEPTION(Error("Duplicate \"unix\" section")); |
| 554 | } |
| 555 | hasSeenUnix = true; |
| 556 | |
| 557 | processSectionUnix(item.second, isDryRun); |
| 558 | } |
| 559 | else if (item.first == "tcp") { |
| 560 | if (hasSeenTcp) { |
| 561 | BOOST_THROW_EXCEPTION(Error("Duplicate \"tcp\" section")); |
| 562 | } |
| 563 | hasSeenTcp = true; |
| 564 | |
| 565 | processSectionTcp(item.second, isDryRun); |
| 566 | } |
| 567 | else if (item.first == "udp") { |
| 568 | if (hasSeenUdp) { |
| 569 | BOOST_THROW_EXCEPTION(Error("Duplicate \"udp\" section")); |
| 570 | } |
| 571 | hasSeenUdp = true; |
| 572 | |
| 573 | processSectionUdp(item.second, isDryRun, nicList); |
| 574 | } |
| 575 | else if (item.first == "ether") { |
| 576 | if (hasSeenEther) { |
| 577 | BOOST_THROW_EXCEPTION(Error("Duplicate \"ether\" section")); |
| 578 | } |
| 579 | hasSeenEther = true; |
| 580 | |
| 581 | processSectionEther(item.second, isDryRun, nicList); |
| 582 | } |
| 583 | else if (item.first == "websocket") { |
| 584 | if (hasSeenWebSocket) { |
| 585 | BOOST_THROW_EXCEPTION(Error("Duplicate \"websocket\" section")); |
| 586 | } |
| 587 | hasSeenWebSocket = true; |
| 588 | |
| 589 | processSectionWebSocket(item.second, isDryRun); |
| 590 | } |
| 591 | else { |
| 592 | BOOST_THROW_EXCEPTION(Error("Unrecognized option \"" + item.first + "\"")); |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | void |
| 598 | FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun) |
| 599 | { |
| 600 | // ; the unix section contains settings of Unix stream faces and channels |
| 601 | // unix |
| 602 | // { |
| 603 | // path /var/run/nfd.sock ; Unix stream listener path |
| 604 | // } |
| 605 | |
| 606 | #if defined(HAVE_UNIX_SOCKETS) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 607 | std::string path = "/var/run/nfd.sock"; |
| 608 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 609 | for (const auto& i : configSection) { |
| 610 | if (i.first == "path") { |
| 611 | path = i.second.get_value<std::string>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 612 | } |
| 613 | else { |
| 614 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 615 | i.first + "\" in \"unix\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
| 619 | if (!isDryRun) { |
| 620 | if (m_factories.count("unix") > 0) { |
| 621 | return; |
| 622 | } |
| 623 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 624 | auto factory = make_shared<UnixStreamFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 625 | m_factories.insert(std::make_pair("unix", factory)); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 626 | |
| 627 | auto channel = factory->createChannel(path); |
| 628 | channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 629 | } |
| 630 | #else |
| 631 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, " |
| 632 | "cannot process \"unix\" section")); |
| 633 | #endif // HAVE_UNIX_SOCKETS |
| 634 | } |
| 635 | |
| 636 | void |
| 637 | FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun) |
| 638 | { |
| 639 | // ; the tcp section contains settings of TCP faces and channels |
| 640 | // tcp |
| 641 | // { |
| 642 | // listen yes ; set to 'no' to disable TCP listener, default 'yes' |
| 643 | // port 6363 ; TCP listener port number |
| 644 | // } |
| 645 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 646 | uint16_t port = 6363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 647 | bool needToListen = true; |
| 648 | bool enableV4 = true; |
| 649 | bool enableV6 = true; |
| 650 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 651 | for (const auto& i : configSection) { |
| 652 | if (i.first == "port") { |
| 653 | port = ConfigFile::parseNumber<uint16_t>(i, "tcp"); |
| 654 | NFD_LOG_TRACE("TCP port set to " << port); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 655 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 656 | else if (i.first == "listen") { |
| 657 | needToListen = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 658 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 659 | else if (i.first == "enable_v4") { |
| 660 | enableV4 = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 661 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 662 | else if (i.first == "enable_v6") { |
| 663 | enableV6 = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 664 | } |
| 665 | else { |
| 666 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 667 | i.first + "\" in \"tcp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
| 671 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 672 | 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] | 673 | " Remove \"tcp\" section to disable TCP channels or" |
| 674 | " re-enable at least one channel type.")); |
| 675 | } |
| 676 | |
| 677 | if (!isDryRun) { |
| 678 | if (m_factories.count("tcp") > 0) { |
| 679 | return; |
| 680 | } |
| 681 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 682 | auto factory = make_shared<TcpFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 683 | m_factories.insert(std::make_pair("tcp", factory)); |
| 684 | |
| 685 | if (enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 686 | tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port); |
| 687 | shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 688 | if (needToListen) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 689 | v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | m_factories.insert(std::make_pair("tcp4", factory)); |
| 693 | } |
| 694 | |
| 695 | if (enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 696 | tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port); |
| 697 | shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 698 | if (needToListen) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 699 | v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | m_factories.insert(std::make_pair("tcp6", factory)); |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 708 | FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 709 | const std::vector<NetworkInterfaceInfo>& nicList) |
| 710 | { |
| 711 | // ; the udp section contains settings of UDP faces and channels |
| 712 | // udp |
| 713 | // { |
| 714 | // port 6363 ; UDP unicast port number |
| 715 | // idle_timeout 600 ; idle time (seconds) before closing a UDP unicast face |
| 716 | // keep_alive_interval 25 ; interval (seconds) between keep-alive refreshes |
| 717 | |
| 718 | // ; NFD creates one UDP multicast face per NIC |
| 719 | // mcast yes ; set to 'no' to disable UDP multicast, default 'yes' |
| 720 | // mcast_port 56363 ; UDP multicast port number |
| 721 | // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only) |
| 722 | // } |
| 723 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 724 | uint16_t port = 6363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 725 | bool enableV4 = true; |
| 726 | bool enableV6 = true; |
| 727 | size_t timeout = 600; |
| 728 | size_t keepAliveInterval = 25; |
| 729 | bool useMcast = true; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 730 | auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170"); |
| 731 | uint16_t mcastPort = 56363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 732 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 733 | for (const auto& i : configSection) { |
| 734 | if (i.first == "port") { |
| 735 | port = ConfigFile::parseNumber<uint16_t>(i, "udp"); |
| 736 | NFD_LOG_TRACE("UDP unicast port set to " << port); |
| 737 | } |
| 738 | else if (i.first == "enable_v4") { |
| 739 | enableV4 = ConfigFile::parseYesNo(i, "udp"); |
| 740 | } |
| 741 | else if (i.first == "enable_v6") { |
| 742 | enableV6 = ConfigFile::parseYesNo(i, "udp"); |
| 743 | } |
| 744 | else if (i.first == "idle_timeout") { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 745 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 746 | timeout = i.second.get_value<size_t>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 747 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 748 | catch (const boost::property_tree::ptree_bad_data&) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 749 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 750 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 751 | } |
| 752 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 753 | else if (i.first == "keep_alive_interval") { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 754 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 755 | keepAliveInterval = i.second.get_value<size_t>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 756 | /// \todo Make use of keepAliveInterval |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 757 | (void)(keepAliveInterval); |
| 758 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 759 | catch (const boost::property_tree::ptree_bad_data&) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 760 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 761 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 764 | else if (i.first == "mcast") { |
| 765 | useMcast = ConfigFile::parseYesNo(i, "udp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 766 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 767 | else if (i.first == "mcast_port") { |
| 768 | mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp"); |
| 769 | NFD_LOG_TRACE("UDP multicast port set to " << mcastPort); |
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 | else if (i.first == "mcast_group") { |
| 772 | boost::system::error_code ec; |
| 773 | mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec); |
| 774 | if (ec) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 775 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 776 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 777 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 778 | NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 779 | } |
| 780 | else { |
| 781 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 782 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
| 786 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 787 | 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] | 788 | " Remove \"udp\" section to disable UDP channels or" |
| 789 | " re-enable at least one channel type.")); |
| 790 | } |
| 791 | else if (useMcast && !enableV4) { |
| 792 | BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 multicast requested, but IPv4 channels" |
| 793 | " have been disabled (conflicting configuration options set)")); |
| 794 | } |
| 795 | |
| 796 | if (!isDryRun) { |
| 797 | shared_ptr<UdpFactory> factory; |
| 798 | bool isReload = false; |
| 799 | if (m_factories.count("udp") > 0) { |
| 800 | isReload = true; |
| 801 | factory = static_pointer_cast<UdpFactory>(m_factories["udp"]); |
| 802 | } |
| 803 | else { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 804 | factory = make_shared<UdpFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 805 | m_factories.insert(std::make_pair("udp", factory)); |
| 806 | } |
| 807 | |
| 808 | if (!isReload && enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 809 | udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port); |
| 810 | shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout)); |
| 811 | v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 812 | |
| 813 | m_factories.insert(std::make_pair("udp4", factory)); |
| 814 | } |
| 815 | |
| 816 | if (!isReload && enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 817 | udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port); |
| 818 | shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout)); |
| 819 | v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 820 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 821 | m_factories.insert(std::make_pair("udp6", factory)); |
| 822 | } |
| 823 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 824 | std::set<shared_ptr<Face>> multicastFacesToRemove; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 825 | for (const auto& i : factory->getMulticastFaces()) { |
| 826 | multicastFacesToRemove.insert(i.second); |
| 827 | } |
| 828 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 829 | if (useMcast && enableV4) { |
| 830 | std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces; |
| 831 | for (const auto& nic : nicList) { |
| 832 | if (nic.isUp() && nic.isMulticastCapable() && !nic.ipv4Addresses.empty()) { |
| 833 | ipv4MulticastInterfaces.push_back(nic); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | bool isNicNameNecessary = false; |
| 838 | #if defined(__linux__) |
| 839 | if (ipv4MulticastInterfaces.size() > 1) { |
| 840 | // On Linux if we have more than one MulticastUdpFace |
| 841 | // we need to specify the name of the interface |
| 842 | isNicNameNecessary = true; |
| 843 | } |
| 844 | #endif |
| 845 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 846 | udp::Endpoint mcastEndpoint(mcastGroup, mcastPort); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 847 | for (const auto& nic : ipv4MulticastInterfaces) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 848 | udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort); |
| 849 | auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint, |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 850 | isNicNameNecessary ? nic.name : ""); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 851 | m_faceTable.add(newFace); |
| 852 | multicastFacesToRemove.erase(newFace); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 853 | } |
| 854 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 855 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 856 | for (const auto& face : multicastFacesToRemove) { |
| 857 | face->close(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 863 | FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 864 | const std::vector<NetworkInterfaceInfo>& nicList) |
| 865 | { |
| 866 | // ; the ether section contains settings of Ethernet faces and channels |
| 867 | // ether |
| 868 | // { |
| 869 | // ; NFD creates one Ethernet multicast face per NIC |
| 870 | // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes' |
| 871 | // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group |
| 872 | // } |
| 873 | |
| 874 | #if defined(HAVE_LIBPCAP) |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame^] | 875 | NetworkInterfacePredicate nicPredicate; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 876 | bool useMcast = true; |
| 877 | ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress()); |
| 878 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 879 | for (const auto& i : configSection) { |
| 880 | if (i.first == "mcast") { |
| 881 | useMcast = ConfigFile::parseYesNo(i, "ether"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 882 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 883 | else if (i.first == "mcast_group") { |
| 884 | mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 885 | if (mcastGroup.isNull()) { |
| 886 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 887 | i.first + "\" in \"ether\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 888 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 889 | NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 890 | } |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame^] | 891 | else if (i.first == "whitelist") { |
| 892 | nicPredicate.parseWhitelist(i.second); |
| 893 | } |
| 894 | else if (i.first == "blacklist") { |
| 895 | nicPredicate.parseBlacklist(i.second); |
| 896 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 897 | else { |
| 898 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 899 | i.first + "\" in \"ether\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
| 903 | if (!isDryRun) { |
| 904 | shared_ptr<EthernetFactory> factory; |
| 905 | if (m_factories.count("ether") > 0) { |
| 906 | factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]); |
| 907 | } |
| 908 | else { |
| 909 | factory = make_shared<EthernetFactory>(); |
| 910 | m_factories.insert(std::make_pair("ether", factory)); |
| 911 | } |
| 912 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 913 | std::set<shared_ptr<Face>> multicastFacesToRemove; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 914 | for (const auto& i : factory->getMulticastFaces()) { |
| 915 | multicastFacesToRemove.insert(i.second); |
| 916 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 917 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 918 | if (useMcast) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 919 | for (const auto& nic : nicList) { |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame^] | 920 | if (nic.isUp() && nic.isMulticastCapable() && nicPredicate(nic)) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 921 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 922 | auto newFace = factory->createMulticastFace(nic, mcastGroup); |
| 923 | m_faceTable.add(newFace); |
| 924 | multicastFacesToRemove.erase(newFace); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 925 | } |
| 926 | catch (const EthernetFactory::Error& factoryError) { |
| 927 | NFD_LOG_ERROR(factoryError.what() << ", continuing"); |
| 928 | } |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 929 | catch (const face::EthernetTransport::Error& faceError) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 930 | NFD_LOG_ERROR(faceError.what() << ", continuing"); |
| 931 | } |
| 932 | } |
| 933 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 934 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 935 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 936 | for (const auto& face : multicastFacesToRemove) { |
| 937 | face->close(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | #else |
| 941 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section")); |
| 942 | #endif // HAVE_LIBPCAP |
| 943 | } |
| 944 | |
| 945 | void |
| 946 | FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun) |
| 947 | { |
| 948 | // ; the websocket section contains settings of WebSocket faces and channels |
| 949 | // websocket |
| 950 | // { |
| 951 | // listen yes ; set to 'no' to disable WebSocket listener, default 'yes' |
| 952 | // port 9696 ; WebSocket listener port number |
| 953 | // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes' |
| 954 | // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes' |
| 955 | // } |
| 956 | |
| 957 | #if defined(HAVE_WEBSOCKET) |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 958 | uint16_t port = 9696; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 959 | bool needToListen = true; |
| 960 | bool enableV4 = true; |
| 961 | bool enableV6 = true; |
| 962 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 963 | for (const auto& i : configSection) { |
| 964 | if (i.first == "port") { |
| 965 | port = ConfigFile::parseNumber<uint16_t>(i, "websocket"); |
| 966 | NFD_LOG_TRACE("WebSocket port set to " << port); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 967 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 968 | else if (i.first == "listen") { |
| 969 | needToListen = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 970 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 971 | else if (i.first == "enable_v4") { |
| 972 | enableV4 = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 973 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 974 | else if (i.first == "enable_v6") { |
| 975 | enableV6 = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 976 | } |
| 977 | else { |
| 978 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 979 | i.first + "\" in \"websocket\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | |
| 983 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 984 | 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] | 985 | " Remove \"websocket\" section to disable WebSocket channels or" |
| 986 | " re-enable at least one channel type.")); |
| 987 | } |
| 988 | |
| 989 | if (!enableV4 && enableV6) { |
| 990 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel.")); |
| 991 | } |
| 992 | |
| 993 | if (!isDryRun) { |
| 994 | if (m_factories.count("websocket") > 0) { |
| 995 | return; |
| 996 | } |
| 997 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 998 | auto factory = make_shared<WebSocketFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 999 | m_factories.insert(std::make_pair("websocket", factory)); |
| 1000 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 1001 | shared_ptr<WebSocketChannel> channel; |
| 1002 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1003 | if (enableV6 && enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 1004 | websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port); |
| 1005 | channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1006 | |
| 1007 | m_factories.insert(std::make_pair("websocket46", factory)); |
| 1008 | } |
| 1009 | else if (enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 1010 | websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port); |
| 1011 | channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1012 | |
| 1013 | m_factories.insert(std::make_pair("websocket4", factory)); |
| 1014 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 1015 | |
| 1016 | if (channel && needToListen) { |
| 1017 | channel->listen(bind(&FaceTable::add, &m_faceTable, _1)); |
| 1018 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1019 | } |
| 1020 | #else |
| 1021 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, " |
| 1022 | "cannot process \"websocket\" section")); |
| 1023 | #endif // HAVE_WEBSOCKET |
| 1024 | } |
| 1025 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 1026 | } // namespace nfd |