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