Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "face-manager.hpp" |
| 27 | |
| 28 | #include "core/network-interface.hpp" |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 29 | #include "face/generic-link-service.hpp" |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 30 | #include "face/tcp-factory.hpp" |
| 31 | #include "face/udp-factory.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 32 | #include "fw/face-table.hpp" |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 33 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 34 | #include <ndn-cxx/management/nfd-channel-status.hpp> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 35 | #include <ndn-cxx/management/nfd-face-status.hpp> |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 36 | #include <ndn-cxx/management/nfd-face-event-notification.hpp> |
| 37 | |
| 38 | #ifdef HAVE_UNIX_SOCKETS |
| 39 | #include "face/unix-stream-factory.hpp" |
| 40 | #endif // HAVE_UNIX_SOCKETS |
| 41 | |
| 42 | #ifdef HAVE_LIBPCAP |
| 43 | #include "face/ethernet-factory.hpp" |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 44 | #include "face/ethernet-transport.hpp" |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 45 | #endif // HAVE_LIBPCAP |
| 46 | |
| 47 | #ifdef HAVE_WEBSOCKET |
| 48 | #include "face/websocket-factory.hpp" |
| 49 | #endif // HAVE_WEBSOCKET |
| 50 | |
| 51 | namespace nfd { |
| 52 | |
| 53 | NFD_LOG_INIT("FaceManager"); |
| 54 | |
| 55 | FaceManager::FaceManager(FaceTable& faceTable, |
| 56 | Dispatcher& dispatcher, |
| 57 | CommandValidator& validator) |
| 58 | : ManagerBase(dispatcher, validator, "faces") |
| 59 | , m_faceTable(faceTable) |
| 60 | { |
| 61 | registerCommandHandler<ndn::nfd::FaceCreateCommand>("create", |
| 62 | bind(&FaceManager::createFace, this, _2, _3, _4, _5)); |
| 63 | |
| 64 | registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy", |
| 65 | bind(&FaceManager::destroyFace, this, _2, _3, _4, _5)); |
| 66 | |
| 67 | registerCommandHandler<ndn::nfd::FaceEnableLocalControlCommand>("enable-local-control", |
| 68 | bind(&FaceManager::enableLocalControl, this, _2, _3, _4, _5)); |
| 69 | |
| 70 | registerCommandHandler<ndn::nfd::FaceDisableLocalControlCommand>("disable-local-control", |
| 71 | bind(&FaceManager::disableLocalControl, this, _2, _3, _4, _5)); |
| 72 | |
| 73 | registerStatusDatasetHandler("list", bind(&FaceManager::listFaces, this, _1, _2, _3)); |
| 74 | registerStatusDatasetHandler("channels", bind(&FaceManager::listChannels, this, _1, _2, _3)); |
| 75 | registerStatusDatasetHandler("query", bind(&FaceManager::queryFaces, this, _1, _2, _3)); |
| 76 | |
| 77 | auto postNotification = registerNotificationStream("events"); |
| 78 | m_faceAddConn = |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 79 | m_faceTable.afterAdd.connect(bind(&FaceManager::afterFaceAdded, this, _1, postNotification)); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 80 | m_faceRemoveConn = |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 81 | m_faceTable.beforeRemove.connect(bind(&FaceManager::afterFaceRemoved, this, _1, postNotification)); |
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"); |
| 98 | return done(ControlResponse(400, "Malformed command")); |
| 99 | } |
| 100 | |
| 101 | if (!uri.isCanonical()) { |
| 102 | NFD_LOG_TRACE("received non-canonical URI"); |
| 103 | return done(ControlResponse(400, "Non-canonical URI")); |
| 104 | } |
| 105 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 106 | auto factory = m_factories.find(uri.getScheme()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 107 | if (factory == m_factories.end()) { |
| 108 | return done(ControlResponse(501, "Unsupported protocol")); |
| 109 | } |
| 110 | |
| 111 | try { |
| 112 | factory->second->createFace(uri, |
| 113 | parameters.getFacePersistency(), |
| 114 | bind(&FaceManager::afterCreateFaceSuccess, |
| 115 | this, parameters, _1, done), |
| 116 | bind(&FaceManager::afterCreateFaceFailure, |
| 117 | this, _1, done)); |
| 118 | } |
| 119 | catch (const std::runtime_error& error) { |
| 120 | std::string errorMessage = "Face creation failed: "; |
| 121 | errorMessage += error.what(); |
| 122 | |
| 123 | NFD_LOG_ERROR(errorMessage); |
| 124 | return done(ControlResponse(500, errorMessage)); |
| 125 | } |
| 126 | catch (const std::logic_error& error) { |
| 127 | std::string errorMessage = "Face creation failed: "; |
| 128 | errorMessage += error.what(); |
| 129 | |
| 130 | NFD_LOG_ERROR(errorMessage); |
| 131 | return done(ControlResponse(500, errorMessage)); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 136 | FaceManager::afterCreateFaceSuccess(ControlParameters& parameters, |
| 137 | const shared_ptr<Face>& newFace, |
| 138 | const ndn::mgmt::CommandContinuation& done) |
| 139 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 140 | m_faceTable.add(newFace); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 141 | parameters.setFaceId(newFace->getId()); |
| 142 | parameters.setUri(newFace->getRemoteUri().toString()); |
| 143 | parameters.setFacePersistency(newFace->getPersistency()); |
| 144 | |
| 145 | done(ControlResponse(200, "OK").setBody(parameters.wireEncode())); |
| 146 | } |
| 147 | |
| 148 | void |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 149 | FaceManager::destroyFace(const Name& topPrefix, const Interest& interest, |
| 150 | const ControlParameters& parameters, |
| 151 | const ndn::mgmt::CommandContinuation& done) |
| 152 | { |
| 153 | shared_ptr<Face> target = m_faceTable.get(parameters.getFaceId()); |
| 154 | if (target) { |
| 155 | target->close(); |
| 156 | } |
| 157 | |
| 158 | done(ControlResponse(200, "OK").setBody(parameters.wireEncode())); |
| 159 | } |
| 160 | |
| 161 | void |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 162 | FaceManager::afterCreateFaceFailure(const std::string& reason, |
| 163 | const ndn::mgmt::CommandContinuation& done) |
| 164 | { |
| 165 | NFD_LOG_DEBUG("Failed to create face: " << reason); |
| 166 | |
| 167 | done(ControlResponse(408, "Failed to create face: " + reason)); |
| 168 | } |
| 169 | |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 170 | void |
| 171 | FaceManager::enableLocalControl(const Name& topPrefix, const Interest& interest, |
| 172 | const ControlParameters& parameters, |
| 173 | const ndn::mgmt::CommandContinuation& done) |
| 174 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 175 | Face* face = findFaceForLocalControl(interest, parameters, done); |
| 176 | if (!face) { |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 180 | // TODO#3226 redesign enable-local-control |
| 181 | // For now, enable-local-control will enable all local fields in GenericLinkService. |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 182 | auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService()); |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 183 | if (service == nullptr) { |
| 184 | return done(ControlResponse(503, "LinkService type not supported")); |
| 185 | } |
| 186 | |
| 187 | face::GenericLinkService::Options options = service->getOptions(); |
| 188 | options.allowLocalFields = true; |
| 189 | service->setOptions(options); |
| 190 | |
| 191 | return done(ControlResponse(200, "OK: enable all local fields on GenericLinkService") |
| 192 | .setBody(parameters.wireEncode())); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | FaceManager::disableLocalControl(const Name& topPrefix, const Interest& interest, |
| 197 | const ControlParameters& parameters, |
| 198 | const ndn::mgmt::CommandContinuation& done) |
| 199 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 200 | Face* face = findFaceForLocalControl(interest, parameters, done); |
| 201 | if (!face) { |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 205 | // TODO#3226 redesign disable-local-control |
| 206 | // For now, disable-local-control will disable all local fields in GenericLinkService. |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 207 | auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService()); |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 208 | if (service == nullptr) { |
| 209 | return done(ControlResponse(503, "LinkService type not supported")); |
| 210 | } |
| 211 | |
| 212 | face::GenericLinkService::Options options = service->getOptions(); |
| 213 | options.allowLocalFields = false; |
| 214 | service->setOptions(options); |
| 215 | |
| 216 | return done(ControlResponse(200, "OK: disable all local fields on GenericLinkService") |
| 217 | .setBody(parameters.wireEncode())); |
| 218 | } |
| 219 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 220 | Face* |
| 221 | FaceManager::findFaceForLocalControl(const Interest& request, |
| 222 | const ControlParameters& parameters, |
| 223 | const ndn::mgmt::CommandContinuation& done) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 224 | { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 225 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>(); |
| 226 | // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field", |
| 227 | // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD |
| 228 | // and is initialized synchronously with IncomingFaceId field enabled. |
| 229 | BOOST_ASSERT(incomingFaceIdTag != nullptr); |
| 230 | |
| 231 | auto face = m_faceTable.get(*incomingFaceIdTag); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 232 | if (face == nullptr) { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 233 | NFD_LOG_DEBUG("FaceId " << *incomingFaceIdTag << " not found"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 234 | done(ControlResponse(410, "Face not found")); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 235 | return nullptr; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 238 | if (face->getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 239 | 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] | 240 | done(ControlResponse(412, "Face is non-local")); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 241 | return nullptr; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 244 | return face.get(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void |
| 248 | FaceManager::listFaces(const Name& topPrefix, const Interest& interest, |
| 249 | ndn::mgmt::StatusDatasetContext& context) |
| 250 | { |
| 251 | for (const auto& face : m_faceTable) { |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 252 | ndn::nfd::FaceStatus status; |
| 253 | collectFaceProperties(*face, status); |
| 254 | collectFaceCounters(*face, status); |
| 255 | context.append(status.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 256 | } |
| 257 | context.end(); |
| 258 | } |
| 259 | |
| 260 | void |
| 261 | FaceManager::listChannels(const Name& topPrefix, const Interest& interest, |
| 262 | ndn::mgmt::StatusDatasetContext& context) |
| 263 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 264 | std::set<const ProtocolFactory*> seenFactories; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 265 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 266 | for (const auto& kv : m_factories) { |
| 267 | const ProtocolFactory* factory = kv.second.get(); |
| 268 | bool inserted; |
| 269 | std::tie(std::ignore, inserted) = seenFactories.insert(factory); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 270 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 271 | if (inserted) { |
| 272 | for (const auto& channel : factory->getChannels()) { |
| 273 | ndn::nfd::ChannelStatus entry; |
| 274 | entry.setLocalUri(channel->getUri().toString()); |
| 275 | context.append(entry.wireEncode()); |
| 276 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | context.end(); |
| 281 | } |
| 282 | |
| 283 | void |
| 284 | FaceManager::queryFaces(const Name& topPrefix, const Interest& interest, |
| 285 | ndn::mgmt::StatusDatasetContext& context) |
| 286 | { |
| 287 | ndn::nfd::FaceQueryFilter faceFilter; |
| 288 | const Name& query = interest.getName(); |
| 289 | try { |
| 290 | faceFilter.wireDecode(query[-1].blockFromValue()); |
| 291 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 292 | catch (const tlv::Error& e) { |
| 293 | NFD_LOG_DEBUG("Malformed query filter: " << e.what()); |
| 294 | return context.reject(ControlResponse(400, "Malformed filter")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | for (const auto& face : m_faceTable) { |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 298 | if (!doesMatchFilter(faceFilter, face)) { |
| 299 | continue; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 300 | } |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 301 | ndn::nfd::FaceStatus status; |
| 302 | collectFaceProperties(*face, status); |
| 303 | collectFaceCounters(*face, status); |
| 304 | context.append(status.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 305 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 306 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 307 | context.end(); |
| 308 | } |
| 309 | |
| 310 | bool |
| 311 | FaceManager::doesMatchFilter(const ndn::nfd::FaceQueryFilter& filter, shared_ptr<Face> face) |
| 312 | { |
| 313 | if (filter.hasFaceId() && |
| 314 | filter.getFaceId() != static_cast<uint64_t>(face->getId())) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | if (filter.hasUriScheme() && |
| 319 | filter.getUriScheme() != face->getRemoteUri().getScheme() && |
| 320 | filter.getUriScheme() != face->getLocalUri().getScheme()) { |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | if (filter.hasRemoteUri() && |
| 325 | filter.getRemoteUri() != face->getRemoteUri().toString()) { |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | if (filter.hasLocalUri() && |
| 330 | filter.getLocalUri() != face->getLocalUri().toString()) { |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | if (filter.hasFaceScope() && |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 335 | filter.getFaceScope() != face->getScope()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 336 | return false; |
| 337 | } |
| 338 | |
| 339 | if (filter.hasFacePersistency() && |
| 340 | filter.getFacePersistency() != face->getPersistency()) { |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | if (filter.hasLinkType() && |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 345 | filter.getLinkType() != face->getLinkType()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 346 | return false; |
| 347 | } |
| 348 | |
| 349 | return true; |
| 350 | } |
| 351 | |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 352 | template<typename FaceTraits> |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 353 | void |
| 354 | FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits) |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 355 | { |
| 356 | traits.setFaceId(face.getId()) |
| 357 | .setRemoteUri(face.getRemoteUri().toString()) |
| 358 | .setLocalUri(face.getLocalUri().toString()) |
| 359 | .setFaceScope(face.getScope()) |
| 360 | .setFacePersistency(face.getPersistency()) |
| 361 | .setLinkType(face.getLinkType()); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | void |
| 365 | FaceManager::collectFaceCounters(const Face& face, ndn::nfd::FaceStatus& status) |
| 366 | { |
| 367 | const face::FaceCounters& counters = face.getCounters(); |
| 368 | status.setNInInterests(counters.nInInterests) |
| 369 | .setNOutInterests(counters.nOutInterests) |
| 370 | .setNInDatas(counters.nInData) |
| 371 | .setNOutDatas(counters.nOutData) |
| 372 | .setNInNacks(counters.nInNacks) |
| 373 | .setNOutNacks(counters.nOutNacks) |
| 374 | .setNInBytes(counters.nInBytes) |
| 375 | .setNOutBytes(counters.nOutBytes); |
| 376 | } |
| 377 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 378 | void |
| 379 | FaceManager::afterFaceAdded(shared_ptr<Face> face, |
| 380 | const ndn::mgmt::PostNotification& post) |
| 381 | { |
| 382 | ndn::nfd::FaceEventNotification notification; |
| 383 | notification.setKind(ndn::nfd::FACE_EVENT_CREATED); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 384 | collectFaceProperties(*face, notification); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 385 | |
| 386 | post(notification.wireEncode()); |
| 387 | } |
| 388 | |
| 389 | void |
| 390 | FaceManager::afterFaceRemoved(shared_ptr<Face> face, |
| 391 | const ndn::mgmt::PostNotification& post) |
| 392 | { |
| 393 | ndn::nfd::FaceEventNotification notification; |
| 394 | notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 395 | collectFaceProperties(*face, notification); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 396 | |
| 397 | post(notification.wireEncode()); |
| 398 | } |
| 399 | |
| 400 | void |
| 401 | FaceManager::processConfig(const ConfigSection& configSection, |
| 402 | bool isDryRun, |
| 403 | const std::string& filename) |
| 404 | { |
| 405 | bool hasSeenUnix = false; |
| 406 | bool hasSeenTcp = false; |
| 407 | bool hasSeenUdp = false; |
| 408 | bool hasSeenEther = false; |
| 409 | bool hasSeenWebSocket = false; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 410 | auto nicList = listNetworkInterfaces(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 411 | |
| 412 | for (const auto& item : configSection) { |
| 413 | if (item.first == "unix") { |
| 414 | if (hasSeenUnix) { |
| 415 | BOOST_THROW_EXCEPTION(Error("Duplicate \"unix\" section")); |
| 416 | } |
| 417 | hasSeenUnix = true; |
| 418 | |
| 419 | processSectionUnix(item.second, isDryRun); |
| 420 | } |
| 421 | else if (item.first == "tcp") { |
| 422 | if (hasSeenTcp) { |
| 423 | BOOST_THROW_EXCEPTION(Error("Duplicate \"tcp\" section")); |
| 424 | } |
| 425 | hasSeenTcp = true; |
| 426 | |
| 427 | processSectionTcp(item.second, isDryRun); |
| 428 | } |
| 429 | else if (item.first == "udp") { |
| 430 | if (hasSeenUdp) { |
| 431 | BOOST_THROW_EXCEPTION(Error("Duplicate \"udp\" section")); |
| 432 | } |
| 433 | hasSeenUdp = true; |
| 434 | |
| 435 | processSectionUdp(item.second, isDryRun, nicList); |
| 436 | } |
| 437 | else if (item.first == "ether") { |
| 438 | if (hasSeenEther) { |
| 439 | BOOST_THROW_EXCEPTION(Error("Duplicate \"ether\" section")); |
| 440 | } |
| 441 | hasSeenEther = true; |
| 442 | |
| 443 | processSectionEther(item.second, isDryRun, nicList); |
| 444 | } |
| 445 | else if (item.first == "websocket") { |
| 446 | if (hasSeenWebSocket) { |
| 447 | BOOST_THROW_EXCEPTION(Error("Duplicate \"websocket\" section")); |
| 448 | } |
| 449 | hasSeenWebSocket = true; |
| 450 | |
| 451 | processSectionWebSocket(item.second, isDryRun); |
| 452 | } |
| 453 | else { |
| 454 | BOOST_THROW_EXCEPTION(Error("Unrecognized option \"" + item.first + "\"")); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | void |
| 460 | FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun) |
| 461 | { |
| 462 | // ; the unix section contains settings of Unix stream faces and channels |
| 463 | // unix |
| 464 | // { |
| 465 | // path /var/run/nfd.sock ; Unix stream listener path |
| 466 | // } |
| 467 | |
| 468 | #if defined(HAVE_UNIX_SOCKETS) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 469 | std::string path = "/var/run/nfd.sock"; |
| 470 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 471 | for (const auto& i : configSection) { |
| 472 | if (i.first == "path") { |
| 473 | path = i.second.get_value<std::string>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 474 | } |
| 475 | else { |
| 476 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 477 | i.first + "\" in \"unix\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
| 481 | if (!isDryRun) { |
| 482 | if (m_factories.count("unix") > 0) { |
| 483 | return; |
| 484 | } |
| 485 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 486 | auto factory = make_shared<UnixStreamFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 487 | m_factories.insert(std::make_pair("unix", factory)); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 488 | |
| 489 | auto channel = factory->createChannel(path); |
| 490 | channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 491 | } |
| 492 | #else |
| 493 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, " |
| 494 | "cannot process \"unix\" section")); |
| 495 | #endif // HAVE_UNIX_SOCKETS |
| 496 | } |
| 497 | |
| 498 | void |
| 499 | FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun) |
| 500 | { |
| 501 | // ; the tcp section contains settings of TCP faces and channels |
| 502 | // tcp |
| 503 | // { |
| 504 | // listen yes ; set to 'no' to disable TCP listener, default 'yes' |
| 505 | // port 6363 ; TCP listener port number |
| 506 | // } |
| 507 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 508 | uint16_t port = 6363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 509 | bool needToListen = true; |
| 510 | bool enableV4 = true; |
| 511 | bool enableV6 = true; |
| 512 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 513 | for (const auto& i : configSection) { |
| 514 | if (i.first == "port") { |
| 515 | port = ConfigFile::parseNumber<uint16_t>(i, "tcp"); |
| 516 | NFD_LOG_TRACE("TCP port set to " << port); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 517 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 518 | else if (i.first == "listen") { |
| 519 | needToListen = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 520 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 521 | else if (i.first == "enable_v4") { |
| 522 | enableV4 = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 523 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 524 | else if (i.first == "enable_v6") { |
| 525 | enableV6 = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 526 | } |
| 527 | else { |
| 528 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 529 | i.first + "\" in \"tcp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
| 533 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 534 | 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] | 535 | " Remove \"tcp\" section to disable TCP channels or" |
| 536 | " re-enable at least one channel type.")); |
| 537 | } |
| 538 | |
| 539 | if (!isDryRun) { |
| 540 | if (m_factories.count("tcp") > 0) { |
| 541 | return; |
| 542 | } |
| 543 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 544 | auto factory = make_shared<TcpFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 545 | m_factories.insert(std::make_pair("tcp", factory)); |
| 546 | |
| 547 | if (enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 548 | tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port); |
| 549 | shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 550 | if (needToListen) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 551 | v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | m_factories.insert(std::make_pair("tcp4", factory)); |
| 555 | } |
| 556 | |
| 557 | if (enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 558 | tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port); |
| 559 | shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 560 | if (needToListen) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 561 | v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | m_factories.insert(std::make_pair("tcp6", factory)); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 570 | FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 571 | const std::vector<NetworkInterfaceInfo>& nicList) |
| 572 | { |
| 573 | // ; the udp section contains settings of UDP faces and channels |
| 574 | // udp |
| 575 | // { |
| 576 | // port 6363 ; UDP unicast port number |
| 577 | // idle_timeout 600 ; idle time (seconds) before closing a UDP unicast face |
| 578 | // keep_alive_interval 25 ; interval (seconds) between keep-alive refreshes |
| 579 | |
| 580 | // ; NFD creates one UDP multicast face per NIC |
| 581 | // mcast yes ; set to 'no' to disable UDP multicast, default 'yes' |
| 582 | // mcast_port 56363 ; UDP multicast port number |
| 583 | // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only) |
| 584 | // } |
| 585 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 586 | uint16_t port = 6363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 587 | bool enableV4 = true; |
| 588 | bool enableV6 = true; |
| 589 | size_t timeout = 600; |
| 590 | size_t keepAliveInterval = 25; |
| 591 | bool useMcast = true; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 592 | auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170"); |
| 593 | uint16_t mcastPort = 56363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 594 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 595 | for (const auto& i : configSection) { |
| 596 | if (i.first == "port") { |
| 597 | port = ConfigFile::parseNumber<uint16_t>(i, "udp"); |
| 598 | NFD_LOG_TRACE("UDP unicast port set to " << port); |
| 599 | } |
| 600 | else if (i.first == "enable_v4") { |
| 601 | enableV4 = ConfigFile::parseYesNo(i, "udp"); |
| 602 | } |
| 603 | else if (i.first == "enable_v6") { |
| 604 | enableV6 = ConfigFile::parseYesNo(i, "udp"); |
| 605 | } |
| 606 | else if (i.first == "idle_timeout") { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 607 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 608 | timeout = i.second.get_value<size_t>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 609 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 610 | catch (const boost::property_tree::ptree_bad_data&) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 611 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 612 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 615 | else if (i.first == "keep_alive_interval") { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 616 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 617 | keepAliveInterval = i.second.get_value<size_t>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 618 | /// \todo Make use of keepAliveInterval |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 619 | (void)(keepAliveInterval); |
| 620 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 621 | catch (const boost::property_tree::ptree_bad_data&) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 622 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 623 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 626 | else if (i.first == "mcast") { |
| 627 | useMcast = ConfigFile::parseYesNo(i, "udp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 628 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 629 | else if (i.first == "mcast_port") { |
| 630 | mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp"); |
| 631 | NFD_LOG_TRACE("UDP multicast port set to " << mcastPort); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 632 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 633 | else if (i.first == "mcast_group") { |
| 634 | boost::system::error_code ec; |
| 635 | mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec); |
| 636 | if (ec) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 637 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 638 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 639 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 640 | NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 641 | } |
| 642 | else { |
| 643 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 644 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
| 648 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 649 | 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] | 650 | " Remove \"udp\" section to disable UDP channels or" |
| 651 | " re-enable at least one channel type.")); |
| 652 | } |
| 653 | else if (useMcast && !enableV4) { |
| 654 | BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 multicast requested, but IPv4 channels" |
| 655 | " have been disabled (conflicting configuration options set)")); |
| 656 | } |
| 657 | |
| 658 | if (!isDryRun) { |
| 659 | shared_ptr<UdpFactory> factory; |
| 660 | bool isReload = false; |
| 661 | if (m_factories.count("udp") > 0) { |
| 662 | isReload = true; |
| 663 | factory = static_pointer_cast<UdpFactory>(m_factories["udp"]); |
| 664 | } |
| 665 | else { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 666 | factory = make_shared<UdpFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 667 | m_factories.insert(std::make_pair("udp", factory)); |
| 668 | } |
| 669 | |
| 670 | if (!isReload && enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 671 | udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port); |
| 672 | shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout)); |
| 673 | v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 674 | |
| 675 | m_factories.insert(std::make_pair("udp4", factory)); |
| 676 | } |
| 677 | |
| 678 | if (!isReload && enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 679 | udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port); |
| 680 | shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout)); |
| 681 | v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 682 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 683 | m_factories.insert(std::make_pair("udp6", factory)); |
| 684 | } |
| 685 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 686 | std::set<shared_ptr<Face>> multicastFacesToRemove; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 687 | for (const auto& i : factory->getMulticastFaces()) { |
| 688 | multicastFacesToRemove.insert(i.second); |
| 689 | } |
| 690 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 691 | if (useMcast && enableV4) { |
| 692 | std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces; |
| 693 | for (const auto& nic : nicList) { |
| 694 | if (nic.isUp() && nic.isMulticastCapable() && !nic.ipv4Addresses.empty()) { |
| 695 | ipv4MulticastInterfaces.push_back(nic); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | bool isNicNameNecessary = false; |
| 700 | #if defined(__linux__) |
| 701 | if (ipv4MulticastInterfaces.size() > 1) { |
| 702 | // On Linux if we have more than one MulticastUdpFace |
| 703 | // we need to specify the name of the interface |
| 704 | isNicNameNecessary = true; |
| 705 | } |
| 706 | #endif |
| 707 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 708 | udp::Endpoint mcastEndpoint(mcastGroup, mcastPort); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 709 | for (const auto& nic : ipv4MulticastInterfaces) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 710 | udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort); |
| 711 | auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint, |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 712 | isNicNameNecessary ? nic.name : ""); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 713 | m_faceTable.add(newFace); |
| 714 | multicastFacesToRemove.erase(newFace); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 715 | } |
| 716 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 717 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 718 | for (const auto& face : multicastFacesToRemove) { |
| 719 | face->close(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 725 | FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 726 | const std::vector<NetworkInterfaceInfo>& nicList) |
| 727 | { |
| 728 | // ; the ether section contains settings of Ethernet faces and channels |
| 729 | // ether |
| 730 | // { |
| 731 | // ; NFD creates one Ethernet multicast face per NIC |
| 732 | // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes' |
| 733 | // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group |
| 734 | // } |
| 735 | |
| 736 | #if defined(HAVE_LIBPCAP) |
| 737 | bool useMcast = true; |
| 738 | ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress()); |
| 739 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 740 | for (const auto& i : configSection) { |
| 741 | if (i.first == "mcast") { |
| 742 | useMcast = ConfigFile::parseYesNo(i, "ether"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 743 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 744 | else if (i.first == "mcast_group") { |
| 745 | mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 746 | if (mcastGroup.isNull()) { |
| 747 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 748 | i.first + "\" in \"ether\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 749 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 750 | NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 751 | } |
| 752 | else { |
| 753 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 754 | i.first + "\" in \"ether\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | if (!isDryRun) { |
| 759 | shared_ptr<EthernetFactory> factory; |
| 760 | if (m_factories.count("ether") > 0) { |
| 761 | factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]); |
| 762 | } |
| 763 | else { |
| 764 | factory = make_shared<EthernetFactory>(); |
| 765 | m_factories.insert(std::make_pair("ether", factory)); |
| 766 | } |
| 767 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame^] | 768 | std::set<shared_ptr<Face>> multicastFacesToRemove; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 769 | for (const auto& i : factory->getMulticastFaces()) { |
| 770 | multicastFacesToRemove.insert(i.second); |
| 771 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 772 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 773 | if (useMcast) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 774 | for (const auto& nic : nicList) { |
| 775 | if (nic.isUp() && nic.isMulticastCapable()) { |
| 776 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 777 | auto newFace = factory->createMulticastFace(nic, mcastGroup); |
| 778 | m_faceTable.add(newFace); |
| 779 | multicastFacesToRemove.erase(newFace); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 780 | } |
| 781 | catch (const EthernetFactory::Error& factoryError) { |
| 782 | NFD_LOG_ERROR(factoryError.what() << ", continuing"); |
| 783 | } |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 784 | catch (const face::EthernetTransport::Error& faceError) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 785 | NFD_LOG_ERROR(faceError.what() << ", continuing"); |
| 786 | } |
| 787 | } |
| 788 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 789 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 790 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 791 | for (const auto& face : multicastFacesToRemove) { |
| 792 | face->close(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | #else |
| 796 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section")); |
| 797 | #endif // HAVE_LIBPCAP |
| 798 | } |
| 799 | |
| 800 | void |
| 801 | FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun) |
| 802 | { |
| 803 | // ; the websocket section contains settings of WebSocket faces and channels |
| 804 | // websocket |
| 805 | // { |
| 806 | // listen yes ; set to 'no' to disable WebSocket listener, default 'yes' |
| 807 | // port 9696 ; WebSocket listener port number |
| 808 | // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes' |
| 809 | // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes' |
| 810 | // } |
| 811 | |
| 812 | #if defined(HAVE_WEBSOCKET) |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 813 | uint16_t port = 9696; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 814 | bool needToListen = true; |
| 815 | bool enableV4 = true; |
| 816 | bool enableV6 = true; |
| 817 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 818 | for (const auto& i : configSection) { |
| 819 | if (i.first == "port") { |
| 820 | port = ConfigFile::parseNumber<uint16_t>(i, "websocket"); |
| 821 | NFD_LOG_TRACE("WebSocket port set to " << port); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 822 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 823 | else if (i.first == "listen") { |
| 824 | needToListen = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 825 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 826 | else if (i.first == "enable_v4") { |
| 827 | enableV4 = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 828 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 829 | else if (i.first == "enable_v6") { |
| 830 | enableV6 = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 831 | } |
| 832 | else { |
| 833 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 834 | i.first + "\" in \"websocket\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
| 838 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 839 | 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] | 840 | " Remove \"websocket\" section to disable WebSocket channels or" |
| 841 | " re-enable at least one channel type.")); |
| 842 | } |
| 843 | |
| 844 | if (!enableV4 && enableV6) { |
| 845 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel.")); |
| 846 | } |
| 847 | |
| 848 | if (!isDryRun) { |
| 849 | if (m_factories.count("websocket") > 0) { |
| 850 | return; |
| 851 | } |
| 852 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 853 | auto factory = make_shared<WebSocketFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 854 | m_factories.insert(std::make_pair("websocket", factory)); |
| 855 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 856 | shared_ptr<WebSocketChannel> channel; |
| 857 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 858 | if (enableV6 && enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 859 | websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port); |
| 860 | channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 861 | |
| 862 | m_factories.insert(std::make_pair("websocket46", factory)); |
| 863 | } |
| 864 | else if (enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 865 | websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port); |
| 866 | channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 867 | |
| 868 | m_factories.insert(std::make_pair("websocket4", factory)); |
| 869 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 870 | |
| 871 | if (channel && needToListen) { |
| 872 | channel->listen(bind(&FaceTable::add, &m_faceTable, _1)); |
| 873 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 874 | } |
| 875 | #else |
| 876 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, " |
| 877 | "cannot process \"websocket\" section")); |
| 878 | #endif // HAVE_WEBSOCKET |
| 879 | } |
| 880 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 881 | } // namespace nfd |