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