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