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