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