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 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 35 | #include <ndn-cxx/management/nfd-channel-status.hpp> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 36 | #include <ndn-cxx/management/nfd-face-status.hpp> |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 107 | auto factory = m_factories.find(uri.getScheme()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 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 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 141 | m_faceTable.add(newFace); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 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()); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 243 | if (face == nullptr) { |
| 244 | NFD_LOG_DEBUG("FaceId " << request.getIncomingFaceId() << " not found"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 245 | done(ControlResponse(410, "Face not found")); |
| 246 | return result; |
| 247 | } |
| 248 | |
| 249 | if (!face->isLocal()) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 250 | 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] | 251 | done(ControlResponse(412, "Face is non-local")); |
| 252 | return result; |
| 253 | } |
| 254 | |
| 255 | result.isValid = true; |
| 256 | result.face = dynamic_pointer_cast<LocalFace>(face); |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 257 | if (result.face == nullptr) { |
| 258 | auto lpFaceW = dynamic_pointer_cast<face::LpFaceWrapper>(face); |
| 259 | BOOST_ASSERT(lpFaceW != nullptr); |
| 260 | result.lpFace = lpFaceW->getLpFace(); |
| 261 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 262 | result.feature = parameters.getLocalControlFeature(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 263 | |
| 264 | return result; |
| 265 | } |
| 266 | |
| 267 | void |
| 268 | FaceManager::listFaces(const Name& topPrefix, const Interest& interest, |
| 269 | ndn::mgmt::StatusDatasetContext& context) |
| 270 | { |
| 271 | for (const auto& face : m_faceTable) { |
| 272 | context.append(face->getFaceStatus().wireEncode()); |
| 273 | } |
| 274 | context.end(); |
| 275 | } |
| 276 | |
| 277 | void |
| 278 | FaceManager::listChannels(const Name& topPrefix, const Interest& interest, |
| 279 | ndn::mgmt::StatusDatasetContext& context) |
| 280 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 281 | std::set<const ProtocolFactory*> seenFactories; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 282 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 283 | for (const auto& kv : m_factories) { |
| 284 | const ProtocolFactory* factory = kv.second.get(); |
| 285 | bool inserted; |
| 286 | std::tie(std::ignore, inserted) = seenFactories.insert(factory); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 287 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 288 | if (inserted) { |
| 289 | for (const auto& channel : factory->getChannels()) { |
| 290 | ndn::nfd::ChannelStatus entry; |
| 291 | entry.setLocalUri(channel->getUri().toString()); |
| 292 | context.append(entry.wireEncode()); |
| 293 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | context.end(); |
| 298 | } |
| 299 | |
| 300 | void |
| 301 | FaceManager::queryFaces(const Name& topPrefix, const Interest& interest, |
| 302 | ndn::mgmt::StatusDatasetContext& context) |
| 303 | { |
| 304 | ndn::nfd::FaceQueryFilter faceFilter; |
| 305 | const Name& query = interest.getName(); |
| 306 | try { |
| 307 | faceFilter.wireDecode(query[-1].blockFromValue()); |
| 308 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 309 | catch (const tlv::Error& e) { |
| 310 | NFD_LOG_DEBUG("Malformed query filter: " << e.what()); |
| 311 | return context.reject(ControlResponse(400, "Malformed filter")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | for (const auto& face : m_faceTable) { |
| 315 | if (doesMatchFilter(faceFilter, face)) { |
| 316 | context.append(face->getFaceStatus().wireEncode()); |
| 317 | } |
| 318 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 319 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 320 | context.end(); |
| 321 | } |
| 322 | |
| 323 | bool |
| 324 | FaceManager::doesMatchFilter(const ndn::nfd::FaceQueryFilter& filter, shared_ptr<Face> face) |
| 325 | { |
| 326 | if (filter.hasFaceId() && |
| 327 | filter.getFaceId() != static_cast<uint64_t>(face->getId())) { |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | if (filter.hasUriScheme() && |
| 332 | filter.getUriScheme() != face->getRemoteUri().getScheme() && |
| 333 | filter.getUriScheme() != face->getLocalUri().getScheme()) { |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | if (filter.hasRemoteUri() && |
| 338 | filter.getRemoteUri() != face->getRemoteUri().toString()) { |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | if (filter.hasLocalUri() && |
| 343 | filter.getLocalUri() != face->getLocalUri().toString()) { |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | if (filter.hasFaceScope() && |
| 348 | (filter.getFaceScope() == ndn::nfd::FACE_SCOPE_LOCAL) != face->isLocal()) { |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | if (filter.hasFacePersistency() && |
| 353 | filter.getFacePersistency() != face->getPersistency()) { |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | if (filter.hasLinkType() && |
| 358 | (filter.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) != face->isMultiAccess()) { |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | void |
| 366 | FaceManager::afterFaceAdded(shared_ptr<Face> face, |
| 367 | const ndn::mgmt::PostNotification& post) |
| 368 | { |
| 369 | ndn::nfd::FaceEventNotification notification; |
| 370 | notification.setKind(ndn::nfd::FACE_EVENT_CREATED); |
| 371 | face->copyStatusTo(notification); |
| 372 | |
| 373 | post(notification.wireEncode()); |
| 374 | } |
| 375 | |
| 376 | void |
| 377 | FaceManager::afterFaceRemoved(shared_ptr<Face> face, |
| 378 | const ndn::mgmt::PostNotification& post) |
| 379 | { |
| 380 | ndn::nfd::FaceEventNotification notification; |
| 381 | notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED); |
| 382 | face->copyStatusTo(notification); |
| 383 | |
| 384 | post(notification.wireEncode()); |
| 385 | } |
| 386 | |
| 387 | void |
| 388 | FaceManager::processConfig(const ConfigSection& configSection, |
| 389 | bool isDryRun, |
| 390 | const std::string& filename) |
| 391 | { |
| 392 | bool hasSeenUnix = false; |
| 393 | bool hasSeenTcp = false; |
| 394 | bool hasSeenUdp = false; |
| 395 | bool hasSeenEther = false; |
| 396 | bool hasSeenWebSocket = false; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 397 | auto nicList = listNetworkInterfaces(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 398 | |
| 399 | for (const auto& item : configSection) { |
| 400 | if (item.first == "unix") { |
| 401 | if (hasSeenUnix) { |
| 402 | BOOST_THROW_EXCEPTION(Error("Duplicate \"unix\" section")); |
| 403 | } |
| 404 | hasSeenUnix = true; |
| 405 | |
| 406 | processSectionUnix(item.second, isDryRun); |
| 407 | } |
| 408 | else if (item.first == "tcp") { |
| 409 | if (hasSeenTcp) { |
| 410 | BOOST_THROW_EXCEPTION(Error("Duplicate \"tcp\" section")); |
| 411 | } |
| 412 | hasSeenTcp = true; |
| 413 | |
| 414 | processSectionTcp(item.second, isDryRun); |
| 415 | } |
| 416 | else if (item.first == "udp") { |
| 417 | if (hasSeenUdp) { |
| 418 | BOOST_THROW_EXCEPTION(Error("Duplicate \"udp\" section")); |
| 419 | } |
| 420 | hasSeenUdp = true; |
| 421 | |
| 422 | processSectionUdp(item.second, isDryRun, nicList); |
| 423 | } |
| 424 | else if (item.first == "ether") { |
| 425 | if (hasSeenEther) { |
| 426 | BOOST_THROW_EXCEPTION(Error("Duplicate \"ether\" section")); |
| 427 | } |
| 428 | hasSeenEther = true; |
| 429 | |
| 430 | processSectionEther(item.second, isDryRun, nicList); |
| 431 | } |
| 432 | else if (item.first == "websocket") { |
| 433 | if (hasSeenWebSocket) { |
| 434 | BOOST_THROW_EXCEPTION(Error("Duplicate \"websocket\" section")); |
| 435 | } |
| 436 | hasSeenWebSocket = true; |
| 437 | |
| 438 | processSectionWebSocket(item.second, isDryRun); |
| 439 | } |
| 440 | else { |
| 441 | BOOST_THROW_EXCEPTION(Error("Unrecognized option \"" + item.first + "\"")); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | void |
| 447 | FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun) |
| 448 | { |
| 449 | // ; the unix section contains settings of Unix stream faces and channels |
| 450 | // unix |
| 451 | // { |
| 452 | // path /var/run/nfd.sock ; Unix stream listener path |
| 453 | // } |
| 454 | |
| 455 | #if defined(HAVE_UNIX_SOCKETS) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 456 | std::string path = "/var/run/nfd.sock"; |
| 457 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 458 | for (const auto& i : configSection) { |
| 459 | if (i.first == "path") { |
| 460 | path = i.second.get_value<std::string>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 461 | } |
| 462 | else { |
| 463 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 464 | i.first + "\" in \"unix\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | if (!isDryRun) { |
| 469 | if (m_factories.count("unix") > 0) { |
| 470 | return; |
| 471 | } |
| 472 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 473 | auto factory = make_shared<UnixStreamFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 474 | m_factories.insert(std::make_pair("unix", factory)); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 475 | |
| 476 | auto channel = factory->createChannel(path); |
| 477 | channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 478 | } |
| 479 | #else |
| 480 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, " |
| 481 | "cannot process \"unix\" section")); |
| 482 | #endif // HAVE_UNIX_SOCKETS |
| 483 | } |
| 484 | |
| 485 | void |
| 486 | FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun) |
| 487 | { |
| 488 | // ; the tcp section contains settings of TCP faces and channels |
| 489 | // tcp |
| 490 | // { |
| 491 | // listen yes ; set to 'no' to disable TCP listener, default 'yes' |
| 492 | // port 6363 ; TCP listener port number |
| 493 | // } |
| 494 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 495 | uint16_t port = 6363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 496 | bool needToListen = true; |
| 497 | bool enableV4 = true; |
| 498 | bool enableV6 = true; |
| 499 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 500 | for (const auto& i : configSection) { |
| 501 | if (i.first == "port") { |
| 502 | port = ConfigFile::parseNumber<uint16_t>(i, "tcp"); |
| 503 | NFD_LOG_TRACE("TCP port set to " << port); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 504 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 505 | else if (i.first == "listen") { |
| 506 | needToListen = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 507 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 508 | else if (i.first == "enable_v4") { |
| 509 | enableV4 = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 510 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 511 | else if (i.first == "enable_v6") { |
| 512 | enableV6 = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 513 | } |
| 514 | else { |
| 515 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 516 | i.first + "\" in \"tcp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| 520 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 521 | 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] | 522 | " Remove \"tcp\" section to disable TCP channels or" |
| 523 | " re-enable at least one channel type.")); |
| 524 | } |
| 525 | |
| 526 | if (!isDryRun) { |
| 527 | if (m_factories.count("tcp") > 0) { |
| 528 | return; |
| 529 | } |
| 530 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 531 | auto factory = make_shared<TcpFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 532 | m_factories.insert(std::make_pair("tcp", factory)); |
| 533 | |
| 534 | if (enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 535 | tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port); |
| 536 | shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 537 | if (needToListen) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 538 | v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | m_factories.insert(std::make_pair("tcp4", factory)); |
| 542 | } |
| 543 | |
| 544 | if (enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 545 | tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port); |
| 546 | shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 547 | if (needToListen) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 548 | v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | m_factories.insert(std::make_pair("tcp6", factory)); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 557 | FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 558 | const std::vector<NetworkInterfaceInfo>& nicList) |
| 559 | { |
| 560 | // ; the udp section contains settings of UDP faces and channels |
| 561 | // udp |
| 562 | // { |
| 563 | // port 6363 ; UDP unicast port number |
| 564 | // idle_timeout 600 ; idle time (seconds) before closing a UDP unicast face |
| 565 | // keep_alive_interval 25 ; interval (seconds) between keep-alive refreshes |
| 566 | |
| 567 | // ; NFD creates one UDP multicast face per NIC |
| 568 | // mcast yes ; set to 'no' to disable UDP multicast, default 'yes' |
| 569 | // mcast_port 56363 ; UDP multicast port number |
| 570 | // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only) |
| 571 | // } |
| 572 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 573 | uint16_t port = 6363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 574 | bool enableV4 = true; |
| 575 | bool enableV6 = true; |
| 576 | size_t timeout = 600; |
| 577 | size_t keepAliveInterval = 25; |
| 578 | bool useMcast = true; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 579 | auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170"); |
| 580 | uint16_t mcastPort = 56363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 581 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 582 | for (const auto& i : configSection) { |
| 583 | if (i.first == "port") { |
| 584 | port = ConfigFile::parseNumber<uint16_t>(i, "udp"); |
| 585 | NFD_LOG_TRACE("UDP unicast port set to " << port); |
| 586 | } |
| 587 | else if (i.first == "enable_v4") { |
| 588 | enableV4 = ConfigFile::parseYesNo(i, "udp"); |
| 589 | } |
| 590 | else if (i.first == "enable_v6") { |
| 591 | enableV6 = ConfigFile::parseYesNo(i, "udp"); |
| 592 | } |
| 593 | else if (i.first == "idle_timeout") { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 594 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 595 | timeout = i.second.get_value<size_t>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 596 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 597 | catch (const boost::property_tree::ptree_bad_data&) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 598 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 599 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 602 | else if (i.first == "keep_alive_interval") { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 603 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 604 | keepAliveInterval = i.second.get_value<size_t>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 605 | /// \todo Make use of keepAliveInterval |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 606 | (void)(keepAliveInterval); |
| 607 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 608 | catch (const boost::property_tree::ptree_bad_data&) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 609 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 610 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 611 | } |
| 612 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 613 | else if (i.first == "mcast") { |
| 614 | useMcast = ConfigFile::parseYesNo(i, "udp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 615 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 616 | else if (i.first == "mcast_port") { |
| 617 | mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp"); |
| 618 | NFD_LOG_TRACE("UDP multicast port set to " << mcastPort); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 619 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 620 | else if (i.first == "mcast_group") { |
| 621 | boost::system::error_code ec; |
| 622 | mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec); |
| 623 | if (ec) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 624 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 625 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 626 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 627 | NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 628 | } |
| 629 | else { |
| 630 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 631 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
| 635 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 636 | 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] | 637 | " Remove \"udp\" section to disable UDP channels or" |
| 638 | " re-enable at least one channel type.")); |
| 639 | } |
| 640 | else if (useMcast && !enableV4) { |
| 641 | BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 multicast requested, but IPv4 channels" |
| 642 | " have been disabled (conflicting configuration options set)")); |
| 643 | } |
| 644 | |
| 645 | if (!isDryRun) { |
| 646 | shared_ptr<UdpFactory> factory; |
| 647 | bool isReload = false; |
| 648 | if (m_factories.count("udp") > 0) { |
| 649 | isReload = true; |
| 650 | factory = static_pointer_cast<UdpFactory>(m_factories["udp"]); |
| 651 | } |
| 652 | else { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 653 | factory = make_shared<UdpFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 654 | m_factories.insert(std::make_pair("udp", factory)); |
| 655 | } |
| 656 | |
| 657 | if (!isReload && enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 658 | udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port); |
| 659 | shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout)); |
| 660 | v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 661 | |
| 662 | m_factories.insert(std::make_pair("udp4", factory)); |
| 663 | } |
| 664 | |
| 665 | if (!isReload && enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 666 | udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port); |
| 667 | shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout)); |
| 668 | v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 669 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 670 | m_factories.insert(std::make_pair("udp6", factory)); |
| 671 | } |
| 672 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 673 | std::set<shared_ptr<face::LpFaceWrapper>> multicastFacesToRemove; |
| 674 | for (const auto& i : factory->getMulticastFaces()) { |
| 675 | multicastFacesToRemove.insert(i.second); |
| 676 | } |
| 677 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 678 | if (useMcast && enableV4) { |
| 679 | std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces; |
| 680 | for (const auto& nic : nicList) { |
| 681 | if (nic.isUp() && nic.isMulticastCapable() && !nic.ipv4Addresses.empty()) { |
| 682 | ipv4MulticastInterfaces.push_back(nic); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | bool isNicNameNecessary = false; |
| 687 | #if defined(__linux__) |
| 688 | if (ipv4MulticastInterfaces.size() > 1) { |
| 689 | // On Linux if we have more than one MulticastUdpFace |
| 690 | // we need to specify the name of the interface |
| 691 | isNicNameNecessary = true; |
| 692 | } |
| 693 | #endif |
| 694 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 695 | udp::Endpoint mcastEndpoint(mcastGroup, mcastPort); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 696 | for (const auto& nic : ipv4MulticastInterfaces) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 697 | udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort); |
| 698 | auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint, |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 699 | isNicNameNecessary ? nic.name : ""); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 700 | m_faceTable.add(newFace); |
| 701 | multicastFacesToRemove.erase(newFace); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 702 | } |
| 703 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 704 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 705 | for (const auto& face : multicastFacesToRemove) { |
| 706 | face->close(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 712 | FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 713 | const std::vector<NetworkInterfaceInfo>& nicList) |
| 714 | { |
| 715 | // ; the ether section contains settings of Ethernet faces and channels |
| 716 | // ether |
| 717 | // { |
| 718 | // ; NFD creates one Ethernet multicast face per NIC |
| 719 | // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes' |
| 720 | // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group |
| 721 | // } |
| 722 | |
| 723 | #if defined(HAVE_LIBPCAP) |
| 724 | bool useMcast = true; |
| 725 | ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress()); |
| 726 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 727 | for (const auto& i : configSection) { |
| 728 | if (i.first == "mcast") { |
| 729 | useMcast = ConfigFile::parseYesNo(i, "ether"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 730 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 731 | else if (i.first == "mcast_group") { |
| 732 | mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 733 | if (mcastGroup.isNull()) { |
| 734 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 735 | i.first + "\" in \"ether\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 736 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 737 | NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 738 | } |
| 739 | else { |
| 740 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 741 | i.first + "\" in \"ether\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
| 745 | if (!isDryRun) { |
| 746 | shared_ptr<EthernetFactory> factory; |
| 747 | if (m_factories.count("ether") > 0) { |
| 748 | factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]); |
| 749 | } |
| 750 | else { |
| 751 | factory = make_shared<EthernetFactory>(); |
| 752 | m_factories.insert(std::make_pair("ether", factory)); |
| 753 | } |
| 754 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 755 | std::set<shared_ptr<EthernetFace>> multicastFacesToRemove; |
| 756 | for (const auto& i : factory->getMulticastFaces()) { |
| 757 | multicastFacesToRemove.insert(i.second); |
| 758 | } |
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 | if (useMcast) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 761 | for (const auto& nic : nicList) { |
| 762 | if (nic.isUp() && nic.isMulticastCapable()) { |
| 763 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 764 | auto newFace = factory->createMulticastFace(nic, mcastGroup); |
| 765 | m_faceTable.add(newFace); |
| 766 | multicastFacesToRemove.erase(newFace); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 767 | } |
| 768 | catch (const EthernetFactory::Error& factoryError) { |
| 769 | NFD_LOG_ERROR(factoryError.what() << ", continuing"); |
| 770 | } |
| 771 | catch (const EthernetFace::Error& faceError) { |
| 772 | NFD_LOG_ERROR(faceError.what() << ", continuing"); |
| 773 | } |
| 774 | } |
| 775 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 776 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 777 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 778 | for (const auto& face : multicastFacesToRemove) { |
| 779 | face->close(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | #else |
| 783 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section")); |
| 784 | #endif // HAVE_LIBPCAP |
| 785 | } |
| 786 | |
| 787 | void |
| 788 | FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun) |
| 789 | { |
| 790 | // ; the websocket section contains settings of WebSocket faces and channels |
| 791 | // websocket |
| 792 | // { |
| 793 | // listen yes ; set to 'no' to disable WebSocket listener, default 'yes' |
| 794 | // port 9696 ; WebSocket listener port number |
| 795 | // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes' |
| 796 | // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes' |
| 797 | // } |
| 798 | |
| 799 | #if defined(HAVE_WEBSOCKET) |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 800 | uint16_t port = 9696; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 801 | bool needToListen = true; |
| 802 | bool enableV4 = true; |
| 803 | bool enableV6 = true; |
| 804 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 805 | for (const auto& i : configSection) { |
| 806 | if (i.first == "port") { |
| 807 | port = ConfigFile::parseNumber<uint16_t>(i, "websocket"); |
| 808 | NFD_LOG_TRACE("WebSocket port set to " << port); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 809 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 810 | else if (i.first == "listen") { |
| 811 | needToListen = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 812 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 813 | else if (i.first == "enable_v4") { |
| 814 | enableV4 = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 815 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 816 | else if (i.first == "enable_v6") { |
| 817 | enableV6 = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 818 | } |
| 819 | else { |
| 820 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 821 | i.first + "\" in \"websocket\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
| 825 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 826 | 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] | 827 | " Remove \"websocket\" section to disable WebSocket channels or" |
| 828 | " re-enable at least one channel type.")); |
| 829 | } |
| 830 | |
| 831 | if (!enableV4 && enableV6) { |
| 832 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel.")); |
| 833 | } |
| 834 | |
| 835 | if (!isDryRun) { |
| 836 | if (m_factories.count("websocket") > 0) { |
| 837 | return; |
| 838 | } |
| 839 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 840 | auto factory = make_shared<WebSocketFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 841 | m_factories.insert(std::make_pair("websocket", factory)); |
| 842 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 843 | shared_ptr<WebSocketChannel> channel; |
| 844 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 845 | if (enableV6 && enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 846 | websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port); |
| 847 | channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 848 | |
| 849 | m_factories.insert(std::make_pair("websocket46", factory)); |
| 850 | } |
| 851 | else if (enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 852 | websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port); |
| 853 | channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 854 | |
| 855 | m_factories.insert(std::make_pair("websocket4", factory)); |
| 856 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 857 | |
| 858 | if (channel && needToListen) { |
| 859 | channel->listen(bind(&FaceTable::add, &m_faceTable, _1)); |
| 860 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 861 | } |
| 862 | #else |
| 863 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, " |
| 864 | "cannot process \"websocket\" section")); |
| 865 | #endif // HAVE_WEBSOCKET |
| 866 | } |
| 867 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 868 | } // namespace nfd |