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