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 | |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 181 | // TODO#3226 redesign enable-local-control |
| 182 | // For now, enable-local-control will enable all local fields in GenericLinkService. |
| 183 | BOOST_ASSERT(result.lpFace != nullptr); |
| 184 | auto service = dynamic_cast<face::GenericLinkService*>(result.lpFace->getLinkService()); |
| 185 | if (service == nullptr) { |
| 186 | return done(ControlResponse(503, "LinkService type not supported")); |
| 187 | } |
| 188 | |
| 189 | face::GenericLinkService::Options options = service->getOptions(); |
| 190 | options.allowLocalFields = true; |
| 191 | service->setOptions(options); |
| 192 | |
| 193 | return done(ControlResponse(200, "OK: enable all local fields on GenericLinkService") |
| 194 | .setBody(parameters.wireEncode())); |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | FaceManager::disableLocalControl(const Name& topPrefix, const Interest& interest, |
| 199 | const ControlParameters& parameters, |
| 200 | const ndn::mgmt::CommandContinuation& done) |
| 201 | { |
| 202 | auto result = extractLocalControlParameters(interest, parameters, done); |
| 203 | if (!result.isValid) { |
| 204 | return; |
| 205 | } |
| 206 | |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 207 | // TODO#3226 redesign disable-local-control |
| 208 | // For now, disable-local-control will disable all local fields in GenericLinkService. |
| 209 | BOOST_ASSERT(result.lpFace != nullptr); |
| 210 | auto service = dynamic_cast<face::GenericLinkService*>(result.lpFace->getLinkService()); |
| 211 | if (service == nullptr) { |
| 212 | return done(ControlResponse(503, "LinkService type not supported")); |
| 213 | } |
| 214 | |
| 215 | face::GenericLinkService::Options options = service->getOptions(); |
| 216 | options.allowLocalFields = false; |
| 217 | service->setOptions(options); |
| 218 | |
| 219 | return done(ControlResponse(200, "OK: disable all local fields on GenericLinkService") |
| 220 | .setBody(parameters.wireEncode())); |
| 221 | } |
| 222 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 223 | FaceManager::ExtractLocalControlParametersResult |
| 224 | FaceManager::extractLocalControlParameters(const Interest& request, |
| 225 | const ControlParameters& parameters, |
| 226 | const ndn::mgmt::CommandContinuation& done) |
| 227 | { |
| 228 | ExtractLocalControlParametersResult result; |
| 229 | result.isValid = false; |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 230 | result.lpFace = nullptr; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 231 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame^] | 232 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>(); |
| 233 | // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field", |
| 234 | // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD |
| 235 | // and is initialized synchronously with IncomingFaceId field enabled. |
| 236 | BOOST_ASSERT(incomingFaceIdTag != nullptr); |
| 237 | |
| 238 | auto face = m_faceTable.get(*incomingFaceIdTag); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 239 | if (face == nullptr) { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame^] | 240 | NFD_LOG_DEBUG("FaceId " << *incomingFaceIdTag << " not found"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 241 | done(ControlResponse(410, "Face not found")); |
| 242 | return result; |
| 243 | } |
| 244 | |
| 245 | if (!face->isLocal()) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 246 | 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] | 247 | done(ControlResponse(412, "Face is non-local")); |
| 248 | return result; |
| 249 | } |
| 250 | |
| 251 | result.isValid = true; |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame^] | 252 | auto lpFaceW = dynamic_pointer_cast<face::LpFaceWrapper>(face); |
| 253 | // LocalFace is gone, so a face that is local must be an LpFace. |
| 254 | BOOST_ASSERT(lpFaceW != nullptr); |
| 255 | result.lpFace = lpFaceW->getLpFace(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 256 | |
| 257 | return result; |
| 258 | } |
| 259 | |
| 260 | void |
| 261 | FaceManager::listFaces(const Name& topPrefix, const Interest& interest, |
| 262 | ndn::mgmt::StatusDatasetContext& context) |
| 263 | { |
| 264 | for (const auto& face : m_faceTable) { |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 265 | ndn::nfd::FaceStatus status; |
| 266 | collectFaceProperties(*face, status); |
| 267 | collectFaceCounters(*face, status); |
| 268 | context.append(status.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 269 | } |
| 270 | context.end(); |
| 271 | } |
| 272 | |
| 273 | void |
| 274 | FaceManager::listChannels(const Name& topPrefix, const Interest& interest, |
| 275 | ndn::mgmt::StatusDatasetContext& context) |
| 276 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 277 | std::set<const ProtocolFactory*> seenFactories; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 278 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 279 | for (const auto& kv : m_factories) { |
| 280 | const ProtocolFactory* factory = kv.second.get(); |
| 281 | bool inserted; |
| 282 | std::tie(std::ignore, inserted) = seenFactories.insert(factory); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 283 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 284 | if (inserted) { |
| 285 | for (const auto& channel : factory->getChannels()) { |
| 286 | ndn::nfd::ChannelStatus entry; |
| 287 | entry.setLocalUri(channel->getUri().toString()); |
| 288 | context.append(entry.wireEncode()); |
| 289 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | context.end(); |
| 294 | } |
| 295 | |
| 296 | void |
| 297 | FaceManager::queryFaces(const Name& topPrefix, const Interest& interest, |
| 298 | ndn::mgmt::StatusDatasetContext& context) |
| 299 | { |
| 300 | ndn::nfd::FaceQueryFilter faceFilter; |
| 301 | const Name& query = interest.getName(); |
| 302 | try { |
| 303 | faceFilter.wireDecode(query[-1].blockFromValue()); |
| 304 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 305 | catch (const tlv::Error& e) { |
| 306 | NFD_LOG_DEBUG("Malformed query filter: " << e.what()); |
| 307 | return context.reject(ControlResponse(400, "Malformed filter")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | for (const auto& face : m_faceTable) { |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 311 | if (!doesMatchFilter(faceFilter, face)) { |
| 312 | continue; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 313 | } |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 314 | ndn::nfd::FaceStatus status; |
| 315 | collectFaceProperties(*face, status); |
| 316 | collectFaceCounters(*face, status); |
| 317 | context.append(status.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 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 | |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 365 | template<typename FaceTraits> |
| 366 | inline void |
| 367 | collectLpFaceProperties(const face::LpFace& face, FaceTraits& traits) |
| 368 | { |
| 369 | traits.setFaceId(face.getId()) |
| 370 | .setRemoteUri(face.getRemoteUri().toString()) |
| 371 | .setLocalUri(face.getLocalUri().toString()) |
| 372 | .setFaceScope(face.getScope()) |
| 373 | .setFacePersistency(face.getPersistency()) |
| 374 | .setLinkType(face.getLinkType()); |
| 375 | // TODO#3172 replace this into FaceManager::collectFaceProperties |
| 376 | } |
| 377 | |
| 378 | template<typename FaceTraits> |
| 379 | void |
| 380 | FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits) |
| 381 | { |
| 382 | auto lpFace = dynamic_cast<const face::LpFace*>(&face); |
| 383 | if (lpFace != nullptr) { |
| 384 | collectLpFaceProperties(*lpFace, traits); |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | traits.setFaceId(face.getId()) |
| 389 | .setRemoteUri(face.getRemoteUri().toString()) |
| 390 | .setLocalUri(face.getLocalUri().toString()) |
| 391 | .setFaceScope(face.isLocal() ? ndn::nfd::FACE_SCOPE_LOCAL |
| 392 | : ndn::nfd::FACE_SCOPE_NON_LOCAL) |
| 393 | .setFacePersistency(face.getPersistency()) |
| 394 | .setLinkType(face.isMultiAccess() ? ndn::nfd::LINK_TYPE_MULTI_ACCESS |
| 395 | : ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 396 | } |
| 397 | |
| 398 | void |
| 399 | FaceManager::collectFaceCounters(const Face& face, ndn::nfd::FaceStatus& status) |
| 400 | { |
| 401 | const face::FaceCounters& counters = face.getCounters(); |
| 402 | status.setNInInterests(counters.nInInterests) |
| 403 | .setNOutInterests(counters.nOutInterests) |
| 404 | .setNInDatas(counters.nInData) |
| 405 | .setNOutDatas(counters.nOutData) |
| 406 | .setNInNacks(counters.nInNacks) |
| 407 | .setNOutNacks(counters.nOutNacks) |
| 408 | .setNInBytes(counters.nInBytes) |
| 409 | .setNOutBytes(counters.nOutBytes); |
| 410 | } |
| 411 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 412 | void |
| 413 | FaceManager::afterFaceAdded(shared_ptr<Face> face, |
| 414 | const ndn::mgmt::PostNotification& post) |
| 415 | { |
| 416 | ndn::nfd::FaceEventNotification notification; |
| 417 | notification.setKind(ndn::nfd::FACE_EVENT_CREATED); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 418 | collectFaceProperties(*face, notification); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 419 | |
| 420 | post(notification.wireEncode()); |
| 421 | } |
| 422 | |
| 423 | void |
| 424 | FaceManager::afterFaceRemoved(shared_ptr<Face> face, |
| 425 | const ndn::mgmt::PostNotification& post) |
| 426 | { |
| 427 | ndn::nfd::FaceEventNotification notification; |
| 428 | notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 429 | collectFaceProperties(*face, notification); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 430 | |
| 431 | post(notification.wireEncode()); |
| 432 | } |
| 433 | |
| 434 | void |
| 435 | FaceManager::processConfig(const ConfigSection& configSection, |
| 436 | bool isDryRun, |
| 437 | const std::string& filename) |
| 438 | { |
| 439 | bool hasSeenUnix = false; |
| 440 | bool hasSeenTcp = false; |
| 441 | bool hasSeenUdp = false; |
| 442 | bool hasSeenEther = false; |
| 443 | bool hasSeenWebSocket = false; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 444 | auto nicList = listNetworkInterfaces(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 445 | |
| 446 | for (const auto& item : configSection) { |
| 447 | if (item.first == "unix") { |
| 448 | if (hasSeenUnix) { |
| 449 | BOOST_THROW_EXCEPTION(Error("Duplicate \"unix\" section")); |
| 450 | } |
| 451 | hasSeenUnix = true; |
| 452 | |
| 453 | processSectionUnix(item.second, isDryRun); |
| 454 | } |
| 455 | else if (item.first == "tcp") { |
| 456 | if (hasSeenTcp) { |
| 457 | BOOST_THROW_EXCEPTION(Error("Duplicate \"tcp\" section")); |
| 458 | } |
| 459 | hasSeenTcp = true; |
| 460 | |
| 461 | processSectionTcp(item.second, isDryRun); |
| 462 | } |
| 463 | else if (item.first == "udp") { |
| 464 | if (hasSeenUdp) { |
| 465 | BOOST_THROW_EXCEPTION(Error("Duplicate \"udp\" section")); |
| 466 | } |
| 467 | hasSeenUdp = true; |
| 468 | |
| 469 | processSectionUdp(item.second, isDryRun, nicList); |
| 470 | } |
| 471 | else if (item.first == "ether") { |
| 472 | if (hasSeenEther) { |
| 473 | BOOST_THROW_EXCEPTION(Error("Duplicate \"ether\" section")); |
| 474 | } |
| 475 | hasSeenEther = true; |
| 476 | |
| 477 | processSectionEther(item.second, isDryRun, nicList); |
| 478 | } |
| 479 | else if (item.first == "websocket") { |
| 480 | if (hasSeenWebSocket) { |
| 481 | BOOST_THROW_EXCEPTION(Error("Duplicate \"websocket\" section")); |
| 482 | } |
| 483 | hasSeenWebSocket = true; |
| 484 | |
| 485 | processSectionWebSocket(item.second, isDryRun); |
| 486 | } |
| 487 | else { |
| 488 | BOOST_THROW_EXCEPTION(Error("Unrecognized option \"" + item.first + "\"")); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | void |
| 494 | FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun) |
| 495 | { |
| 496 | // ; the unix section contains settings of Unix stream faces and channels |
| 497 | // unix |
| 498 | // { |
| 499 | // path /var/run/nfd.sock ; Unix stream listener path |
| 500 | // } |
| 501 | |
| 502 | #if defined(HAVE_UNIX_SOCKETS) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 503 | std::string path = "/var/run/nfd.sock"; |
| 504 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 505 | for (const auto& i : configSection) { |
| 506 | if (i.first == "path") { |
| 507 | path = i.second.get_value<std::string>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 508 | } |
| 509 | else { |
| 510 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 511 | i.first + "\" in \"unix\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
| 515 | if (!isDryRun) { |
| 516 | if (m_factories.count("unix") > 0) { |
| 517 | return; |
| 518 | } |
| 519 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 520 | auto factory = make_shared<UnixStreamFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 521 | m_factories.insert(std::make_pair("unix", factory)); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 522 | |
| 523 | auto channel = factory->createChannel(path); |
| 524 | channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 525 | } |
| 526 | #else |
| 527 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, " |
| 528 | "cannot process \"unix\" section")); |
| 529 | #endif // HAVE_UNIX_SOCKETS |
| 530 | } |
| 531 | |
| 532 | void |
| 533 | FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun) |
| 534 | { |
| 535 | // ; the tcp section contains settings of TCP faces and channels |
| 536 | // tcp |
| 537 | // { |
| 538 | // listen yes ; set to 'no' to disable TCP listener, default 'yes' |
| 539 | // port 6363 ; TCP listener port number |
| 540 | // } |
| 541 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 542 | uint16_t port = 6363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 543 | bool needToListen = true; |
| 544 | bool enableV4 = true; |
| 545 | bool enableV6 = true; |
| 546 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 547 | for (const auto& i : configSection) { |
| 548 | if (i.first == "port") { |
| 549 | port = ConfigFile::parseNumber<uint16_t>(i, "tcp"); |
| 550 | NFD_LOG_TRACE("TCP port set to " << port); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 551 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 552 | else if (i.first == "listen") { |
| 553 | needToListen = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 554 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 555 | else if (i.first == "enable_v4") { |
| 556 | enableV4 = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 557 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 558 | else if (i.first == "enable_v6") { |
| 559 | enableV6 = ConfigFile::parseYesNo(i, "tcp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 560 | } |
| 561 | else { |
| 562 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 563 | i.first + "\" in \"tcp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
| 567 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 568 | 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] | 569 | " Remove \"tcp\" section to disable TCP channels or" |
| 570 | " re-enable at least one channel type.")); |
| 571 | } |
| 572 | |
| 573 | if (!isDryRun) { |
| 574 | if (m_factories.count("tcp") > 0) { |
| 575 | return; |
| 576 | } |
| 577 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 578 | auto factory = make_shared<TcpFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 579 | m_factories.insert(std::make_pair("tcp", factory)); |
| 580 | |
| 581 | if (enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 582 | tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port); |
| 583 | shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 584 | if (needToListen) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 585 | v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | m_factories.insert(std::make_pair("tcp4", factory)); |
| 589 | } |
| 590 | |
| 591 | if (enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 592 | tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port); |
| 593 | shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 594 | if (needToListen) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 595 | v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | m_factories.insert(std::make_pair("tcp6", factory)); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 604 | FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 605 | const std::vector<NetworkInterfaceInfo>& nicList) |
| 606 | { |
| 607 | // ; the udp section contains settings of UDP faces and channels |
| 608 | // udp |
| 609 | // { |
| 610 | // port 6363 ; UDP unicast port number |
| 611 | // idle_timeout 600 ; idle time (seconds) before closing a UDP unicast face |
| 612 | // keep_alive_interval 25 ; interval (seconds) between keep-alive refreshes |
| 613 | |
| 614 | // ; NFD creates one UDP multicast face per NIC |
| 615 | // mcast yes ; set to 'no' to disable UDP multicast, default 'yes' |
| 616 | // mcast_port 56363 ; UDP multicast port number |
| 617 | // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only) |
| 618 | // } |
| 619 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 620 | uint16_t port = 6363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 621 | bool enableV4 = true; |
| 622 | bool enableV6 = true; |
| 623 | size_t timeout = 600; |
| 624 | size_t keepAliveInterval = 25; |
| 625 | bool useMcast = true; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 626 | auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170"); |
| 627 | uint16_t mcastPort = 56363; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 628 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 629 | for (const auto& i : configSection) { |
| 630 | if (i.first == "port") { |
| 631 | port = ConfigFile::parseNumber<uint16_t>(i, "udp"); |
| 632 | NFD_LOG_TRACE("UDP unicast port set to " << port); |
| 633 | } |
| 634 | else if (i.first == "enable_v4") { |
| 635 | enableV4 = ConfigFile::parseYesNo(i, "udp"); |
| 636 | } |
| 637 | else if (i.first == "enable_v6") { |
| 638 | enableV6 = ConfigFile::parseYesNo(i, "udp"); |
| 639 | } |
| 640 | else if (i.first == "idle_timeout") { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 641 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 642 | timeout = i.second.get_value<size_t>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 643 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 644 | catch (const boost::property_tree::ptree_bad_data&) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 645 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 646 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 649 | else if (i.first == "keep_alive_interval") { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 650 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 651 | keepAliveInterval = i.second.get_value<size_t>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 652 | /// \todo Make use of keepAliveInterval |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 653 | (void)(keepAliveInterval); |
| 654 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 655 | catch (const boost::property_tree::ptree_bad_data&) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 656 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 657 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 658 | } |
| 659 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 660 | else if (i.first == "mcast") { |
| 661 | useMcast = ConfigFile::parseYesNo(i, "udp"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 662 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 663 | else if (i.first == "mcast_port") { |
| 664 | mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp"); |
| 665 | NFD_LOG_TRACE("UDP multicast port set to " << mcastPort); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 666 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 667 | else if (i.first == "mcast_group") { |
| 668 | boost::system::error_code ec; |
| 669 | mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec); |
| 670 | if (ec) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 671 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 672 | i.first + "\" in \"udp\" section")); |
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 | NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 675 | } |
| 676 | else { |
| 677 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 678 | i.first + "\" in \"udp\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
| 682 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 683 | 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] | 684 | " Remove \"udp\" section to disable UDP channels or" |
| 685 | " re-enable at least one channel type.")); |
| 686 | } |
| 687 | else if (useMcast && !enableV4) { |
| 688 | BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 multicast requested, but IPv4 channels" |
| 689 | " have been disabled (conflicting configuration options set)")); |
| 690 | } |
| 691 | |
| 692 | if (!isDryRun) { |
| 693 | shared_ptr<UdpFactory> factory; |
| 694 | bool isReload = false; |
| 695 | if (m_factories.count("udp") > 0) { |
| 696 | isReload = true; |
| 697 | factory = static_pointer_cast<UdpFactory>(m_factories["udp"]); |
| 698 | } |
| 699 | else { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 700 | factory = make_shared<UdpFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 701 | m_factories.insert(std::make_pair("udp", factory)); |
| 702 | } |
| 703 | |
| 704 | if (!isReload && enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 705 | udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port); |
| 706 | shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout)); |
| 707 | v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 708 | |
| 709 | m_factories.insert(std::make_pair("udp4", factory)); |
| 710 | } |
| 711 | |
| 712 | if (!isReload && enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 713 | udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port); |
| 714 | shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout)); |
| 715 | v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 716 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 717 | m_factories.insert(std::make_pair("udp6", factory)); |
| 718 | } |
| 719 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 720 | std::set<shared_ptr<face::LpFaceWrapper>> multicastFacesToRemove; |
| 721 | for (const auto& i : factory->getMulticastFaces()) { |
| 722 | multicastFacesToRemove.insert(i.second); |
| 723 | } |
| 724 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 725 | if (useMcast && enableV4) { |
| 726 | std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces; |
| 727 | for (const auto& nic : nicList) { |
| 728 | if (nic.isUp() && nic.isMulticastCapable() && !nic.ipv4Addresses.empty()) { |
| 729 | ipv4MulticastInterfaces.push_back(nic); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | bool isNicNameNecessary = false; |
| 734 | #if defined(__linux__) |
| 735 | if (ipv4MulticastInterfaces.size() > 1) { |
| 736 | // On Linux if we have more than one MulticastUdpFace |
| 737 | // we need to specify the name of the interface |
| 738 | isNicNameNecessary = true; |
| 739 | } |
| 740 | #endif |
| 741 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 742 | udp::Endpoint mcastEndpoint(mcastGroup, mcastPort); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 743 | for (const auto& nic : ipv4MulticastInterfaces) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 744 | udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort); |
| 745 | auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint, |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 746 | isNicNameNecessary ? nic.name : ""); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 747 | m_faceTable.add(newFace); |
| 748 | multicastFacesToRemove.erase(newFace); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 751 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 752 | for (const auto& face : multicastFacesToRemove) { |
| 753 | face->close(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 759 | FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 760 | const std::vector<NetworkInterfaceInfo>& nicList) |
| 761 | { |
| 762 | // ; the ether section contains settings of Ethernet faces and channels |
| 763 | // ether |
| 764 | // { |
| 765 | // ; NFD creates one Ethernet multicast face per NIC |
| 766 | // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes' |
| 767 | // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group |
| 768 | // } |
| 769 | |
| 770 | #if defined(HAVE_LIBPCAP) |
| 771 | bool useMcast = true; |
| 772 | ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress()); |
| 773 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 774 | for (const auto& i : configSection) { |
| 775 | if (i.first == "mcast") { |
| 776 | useMcast = ConfigFile::parseYesNo(i, "ether"); |
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 | else if (i.first == "mcast_group") { |
| 779 | mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 780 | if (mcastGroup.isNull()) { |
| 781 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 782 | i.first + "\" in \"ether\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 783 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 784 | NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 785 | } |
| 786 | else { |
| 787 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 788 | i.first + "\" in \"ether\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
| 792 | if (!isDryRun) { |
| 793 | shared_ptr<EthernetFactory> factory; |
| 794 | if (m_factories.count("ether") > 0) { |
| 795 | factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]); |
| 796 | } |
| 797 | else { |
| 798 | factory = make_shared<EthernetFactory>(); |
| 799 | m_factories.insert(std::make_pair("ether", factory)); |
| 800 | } |
| 801 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 802 | std::set<shared_ptr<face::LpFaceWrapper>> multicastFacesToRemove; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 803 | for (const auto& i : factory->getMulticastFaces()) { |
| 804 | multicastFacesToRemove.insert(i.second); |
| 805 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 806 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 807 | if (useMcast) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 808 | for (const auto& nic : nicList) { |
| 809 | if (nic.isUp() && nic.isMulticastCapable()) { |
| 810 | try { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 811 | auto newFace = factory->createMulticastFace(nic, mcastGroup); |
| 812 | m_faceTable.add(newFace); |
| 813 | multicastFacesToRemove.erase(newFace); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 814 | } |
| 815 | catch (const EthernetFactory::Error& factoryError) { |
| 816 | NFD_LOG_ERROR(factoryError.what() << ", continuing"); |
| 817 | } |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 818 | catch (const face::EthernetTransport::Error& faceError) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 819 | NFD_LOG_ERROR(faceError.what() << ", continuing"); |
| 820 | } |
| 821 | } |
| 822 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 823 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 824 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 825 | for (const auto& face : multicastFacesToRemove) { |
| 826 | face->close(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | #else |
| 830 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section")); |
| 831 | #endif // HAVE_LIBPCAP |
| 832 | } |
| 833 | |
| 834 | void |
| 835 | FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun) |
| 836 | { |
| 837 | // ; the websocket section contains settings of WebSocket faces and channels |
| 838 | // websocket |
| 839 | // { |
| 840 | // listen yes ; set to 'no' to disable WebSocket listener, default 'yes' |
| 841 | // port 9696 ; WebSocket listener port number |
| 842 | // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes' |
| 843 | // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes' |
| 844 | // } |
| 845 | |
| 846 | #if defined(HAVE_WEBSOCKET) |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 847 | uint16_t port = 9696; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 848 | bool needToListen = true; |
| 849 | bool enableV4 = true; |
| 850 | bool enableV6 = true; |
| 851 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 852 | for (const auto& i : configSection) { |
| 853 | if (i.first == "port") { |
| 854 | port = ConfigFile::parseNumber<uint16_t>(i, "websocket"); |
| 855 | NFD_LOG_TRACE("WebSocket port set to " << port); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 856 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 857 | else if (i.first == "listen") { |
| 858 | needToListen = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 859 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 860 | else if (i.first == "enable_v4") { |
| 861 | enableV4 = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 862 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 863 | else if (i.first == "enable_v6") { |
| 864 | enableV6 = ConfigFile::parseYesNo(i, "websocket"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 865 | } |
| 866 | else { |
| 867 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 868 | i.first + "\" in \"websocket\" section")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
| 872 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 873 | 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] | 874 | " Remove \"websocket\" section to disable WebSocket channels or" |
| 875 | " re-enable at least one channel type.")); |
| 876 | } |
| 877 | |
| 878 | if (!enableV4 && enableV6) { |
| 879 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel.")); |
| 880 | } |
| 881 | |
| 882 | if (!isDryRun) { |
| 883 | if (m_factories.count("websocket") > 0) { |
| 884 | return; |
| 885 | } |
| 886 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 887 | auto factory = make_shared<WebSocketFactory>(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 888 | m_factories.insert(std::make_pair("websocket", factory)); |
| 889 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 890 | shared_ptr<WebSocketChannel> channel; |
| 891 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 892 | if (enableV6 && enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 893 | websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port); |
| 894 | channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 895 | |
| 896 | m_factories.insert(std::make_pair("websocket46", factory)); |
| 897 | } |
| 898 | else if (enableV4) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 899 | websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port); |
| 900 | channel = factory->createChannel(endpoint); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 901 | |
| 902 | m_factories.insert(std::make_pair("websocket4", factory)); |
| 903 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 904 | |
| 905 | if (channel && needToListen) { |
| 906 | channel->listen(bind(&FaceTable::add, &m_faceTable, _1)); |
| 907 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 908 | } |
| 909 | #else |
| 910 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, " |
| 911 | "cannot process \"websocket\" section")); |
| 912 | #endif // HAVE_WEBSOCKET |
| 913 | } |
| 914 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 915 | } // namespace nfd |