blob: f41f4dea54a0407592f0757fc4e2496fd9306b1c [file] [log] [blame]
Yanbiao Li73860e32015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Lidf846e52016-01-30 21:53:47 -08003 * Copyright (c) 2014-2016, Regents of the University of California,
Yanbiao Li73860e32015-08-19 16:30:16 -07004 * 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 Shi40cb61c2015-09-23 18:36:45 -070029#include "face/generic-link-service.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070030#include "face/tcp-factory.hpp"
31#include "face/udp-factory.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080032#include "fw/face-table.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070033
Yanbiao Li73860e32015-08-19 16:30:16 -070034#include <ndn-cxx/management/nfd-channel-status.hpp>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020035#include <ndn-cxx/management/nfd-face-status.hpp>
Yanbiao Li73860e32015-08-19 16:30:16 -070036#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 Pesavento35120ea2015-11-17 21:13:18 +010044#include "face/ethernet-transport.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070045#endif // HAVE_LIBPCAP
46
47#ifdef HAVE_WEBSOCKET
48#include "face/websocket-factory.hpp"
49#endif // HAVE_WEBSOCKET
50
51namespace nfd {
52
53NFD_LOG_INIT("FaceManager");
54
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000055FaceManager::FaceManager(FaceTable& faceTable, Dispatcher& dispatcher, CommandAuthenticator& authenticator)
56 : NfdManagerBase(dispatcher, authenticator, "faces")
Yanbiao Li73860e32015-08-19 16:30:16 -070057 , m_faceTable(faceTable)
58{
59 registerCommandHandler<ndn::nfd::FaceCreateCommand>("create",
60 bind(&FaceManager::createFace, this, _2, _3, _4, _5));
61
Eric Newberryb5aa7f52016-09-03 20:36:12 -070062 registerCommandHandler<ndn::nfd::FaceUpdateCommand>("update",
63 bind(&FaceManager::updateFace, this, _2, _3, _4, _5));
64
Yanbiao Li73860e32015-08-19 16:30:16 -070065 registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy",
66 bind(&FaceManager::destroyFace, this, _2, _3, _4, _5));
67
68 registerCommandHandler<ndn::nfd::FaceEnableLocalControlCommand>("enable-local-control",
69 bind(&FaceManager::enableLocalControl, this, _2, _3, _4, _5));
70
71 registerCommandHandler<ndn::nfd::FaceDisableLocalControlCommand>("disable-local-control",
72 bind(&FaceManager::disableLocalControl, this, _2, _3, _4, _5));
73
74 registerStatusDatasetHandler("list", bind(&FaceManager::listFaces, this, _1, _2, _3));
75 registerStatusDatasetHandler("channels", bind(&FaceManager::listChannels, this, _1, _2, _3));
76 registerStatusDatasetHandler("query", bind(&FaceManager::queryFaces, this, _1, _2, _3));
77
Junxiao Shiae04d342016-07-19 13:20:22 +000078 m_postNotification = registerNotificationStream("events");
79 m_faceAddConn = m_faceTable.afterAdd.connect(bind(&FaceManager::notifyAddFace, this, _1));
80 m_faceRemoveConn = m_faceTable.beforeRemove.connect(bind(&FaceManager::notifyRemoveFace, this, _1));
Yanbiao Li73860e32015-08-19 16:30:16 -070081}
82
83void
84FaceManager::setConfigFile(ConfigFile& configFile)
85{
86 configFile.addSectionHandler("face_system", bind(&FaceManager::processConfig, this, _1, _2, _3));
87}
88
89void
90FaceManager::createFace(const Name& topPrefix, const Interest& interest,
91 const ControlParameters& parameters,
92 const ndn::mgmt::CommandContinuation& done)
93{
94 FaceUri uri;
95 if (!uri.parse(parameters.getUri())) {
96 NFD_LOG_TRACE("failed to parse URI");
Eric Newberry42602412016-08-27 09:33:18 -070097 done(ControlResponse(400, "Malformed command"));
98 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070099 }
100
101 if (!uri.isCanonical()) {
102 NFD_LOG_TRACE("received non-canonical URI");
Eric Newberry42602412016-08-27 09:33:18 -0700103 done(ControlResponse(400, "Non-canonical URI"));
104 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700105 }
106
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200107 auto factory = m_factories.find(uri.getScheme());
Yanbiao Li73860e32015-08-19 16:30:16 -0700108 if (factory == m_factories.end()) {
Eric Newberry42602412016-08-27 09:33:18 -0700109 NFD_LOG_TRACE("received create request for unsupported protocol");
110 done(ControlResponse(406, "Unsupported protocol"));
111 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700112 }
113
114 try {
115 factory->second->createFace(uri,
116 parameters.getFacePersistency(),
117 bind(&FaceManager::afterCreateFaceSuccess,
118 this, parameters, _1, done),
119 bind(&FaceManager::afterCreateFaceFailure,
Eric Newberry42602412016-08-27 09:33:18 -0700120 this, _1, _2, done));
Yanbiao Li73860e32015-08-19 16:30:16 -0700121 }
122 catch (const std::runtime_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700123 NFD_LOG_ERROR("Face creation failed: " << error.what());
124 done(ControlResponse(500, "Face creation failed due to internal error"));
125 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700126 }
127 catch (const std::logic_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700128 NFD_LOG_ERROR("Face creation failed: " << error.what());
129 done(ControlResponse(500, "Face creation failed due to internal error"));
130 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700131 }
132}
133
Eric Newberry42602412016-08-27 09:33:18 -0700134/**
135 * \todo #3232
136 * If the creation of this face would conflict with an existing face (e.g. same underlying
137 * protocol and remote address, or a NIC-associated permanent face), the command will fail
138 * with StatusCode 409.
139 */
Yanbiao Li73860e32015-08-19 16:30:16 -0700140void
Eric Newberry42602412016-08-27 09:33:18 -0700141FaceManager::afterCreateFaceSuccess(const ControlParameters& parameters,
Yanbiao Li73860e32015-08-19 16:30:16 -0700142 const shared_ptr<Face>& newFace,
143 const ndn::mgmt::CommandContinuation& done)
144{
Eric Newberry42602412016-08-27 09:33:18 -0700145 ControlParameters response;
Yanbiao Li73860e32015-08-19 16:30:16 -0700146
Eric Newberry42602412016-08-27 09:33:18 -0700147 m_faceTable.add(newFace);
148
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700149 // TODO: #3731: Verify and add Flags
150
Eric Newberry42602412016-08-27 09:33:18 -0700151 // Set ControlResponse parameters
152 response.setFaceId(newFace->getId());
153 response.setFacePersistency(newFace->getPersistency());
154
155 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
156}
157
158void
159FaceManager::afterCreateFaceFailure(uint32_t status,
160 const std::string& reason,
161 const ndn::mgmt::CommandContinuation& done)
162{
163 NFD_LOG_DEBUG("Face creation failed: " << reason);
164
165 done(ControlResponse(status, reason));
Yanbiao Li73860e32015-08-19 16:30:16 -0700166}
167
168void
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700169FaceManager::updateFace(const Name& topPrefix, const Interest& interest,
170 const ControlParameters& parameters,
171 const ndn::mgmt::CommandContinuation& done)
172{
173 FaceId faceId = parameters.getFaceId();
174 if (faceId == 0) {
175 // Self-updating
176 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>();
177 if (incomingFaceIdTag == nullptr) {
178 NFD_LOG_TRACE("unable to determine face for self-update");
179 done(ControlResponse(404, "No FaceId specified and IncomingFaceId not available"));
180 return;
181 }
182 faceId = *incomingFaceIdTag;
183 }
184
185 Face* face = m_faceTable.get(faceId);
186
187 if (face == nullptr) {
188 NFD_LOG_TRACE("invalid face specified");
189 done(ControlResponse(404, "Specified face does not exist"));
190 return;
191 }
192
193 // Verify validity of requested changes
194 ControlParameters response;
195 bool areParamsValid = true;
196
197 if (parameters.hasFacePersistency()) {
198 // TODO #3232: Add FacePersistency updating
199 NFD_LOG_TRACE("received unsupported face persistency change");
200 areParamsValid = false;
201 response.setFacePersistency(parameters.getFacePersistency());
202 }
203
204 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
205 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
206 face->getScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
207 NFD_LOG_TRACE("received request to enable local fields on non-local face");
208 areParamsValid = false;
209 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
210 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
211 }
212
213 if (!areParamsValid) {
214 done(ControlResponse(409, "Invalid properties specified").setBody(response.wireEncode()));
215 return;
216 }
217
218 // All specified properties are valid, so make changes
219
220 // TODO #3232: Add FacePersistency updating
221
222 setLinkServiceOptions(*face, parameters, response);
223
224 // Set remaining ControlResponse fields
225 response.setFaceId(faceId);
226 response.setFacePersistency(face->getPersistency());
227
228 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
229}
230
231void
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700232FaceManager::destroyFace(const Name& topPrefix, const Interest& interest,
233 const ControlParameters& parameters,
234 const ndn::mgmt::CommandContinuation& done)
235{
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000236 Face* face = m_faceTable.get(parameters.getFaceId());
237 if (face != nullptr) {
238 face->close();
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700239 }
240
241 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
242}
243
244void
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700245FaceManager::enableLocalControl(const Name& topPrefix, const Interest& interest,
246 const ControlParameters& parameters,
247 const ndn::mgmt::CommandContinuation& done)
248{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700249 Face* face = findFaceForLocalControl(interest, parameters, done);
250 if (!face) {
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700251 return;
252 }
253
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700254 // enable-local-control will enable all local fields in GenericLinkService
Junxiao Shicde37ad2015-12-24 01:02:05 -0700255 auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService());
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700256 if (service == nullptr) {
257 return done(ControlResponse(503, "LinkService type not supported"));
258 }
259
260 face::GenericLinkService::Options options = service->getOptions();
261 options.allowLocalFields = true;
262 service->setOptions(options);
263
264 return done(ControlResponse(200, "OK: enable all local fields on GenericLinkService")
265 .setBody(parameters.wireEncode()));
266}
267
268void
269FaceManager::disableLocalControl(const Name& topPrefix, const Interest& interest,
270 const ControlParameters& parameters,
271 const ndn::mgmt::CommandContinuation& done)
272{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700273 Face* face = findFaceForLocalControl(interest, parameters, done);
274 if (!face) {
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700275 return;
276 }
277
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700278 // disable-local-control will disable all local fields in GenericLinkService
Junxiao Shicde37ad2015-12-24 01:02:05 -0700279 auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService());
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700280 if (service == nullptr) {
281 return done(ControlResponse(503, "LinkService type not supported"));
282 }
283
284 face::GenericLinkService::Options options = service->getOptions();
285 options.allowLocalFields = false;
286 service->setOptions(options);
287
288 return done(ControlResponse(200, "OK: disable all local fields on GenericLinkService")
289 .setBody(parameters.wireEncode()));
290}
291
Junxiao Shicde37ad2015-12-24 01:02:05 -0700292Face*
293FaceManager::findFaceForLocalControl(const Interest& request,
294 const ControlParameters& parameters,
295 const ndn::mgmt::CommandContinuation& done)
Yanbiao Li73860e32015-08-19 16:30:16 -0700296{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000297 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
298 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
299 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
300 // and is initialized synchronously with IncomingFaceId field enabled.
301 BOOST_ASSERT(incomingFaceIdTag != nullptr);
302
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000303 Face* face = m_faceTable.get(*incomingFaceIdTag);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200304 if (face == nullptr) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000305 NFD_LOG_DEBUG("FaceId " << *incomingFaceIdTag << " not found");
Yanbiao Li73860e32015-08-19 16:30:16 -0700306 done(ControlResponse(410, "Face not found"));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700307 return nullptr;
Yanbiao Li73860e32015-08-19 16:30:16 -0700308 }
309
Junxiao Shicde37ad2015-12-24 01:02:05 -0700310 if (face->getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200311 NFD_LOG_DEBUG("Cannot enable local control on non-local FaceId " << face->getId());
Yanbiao Li73860e32015-08-19 16:30:16 -0700312 done(ControlResponse(412, "Face is non-local"));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700313 return nullptr;
Yanbiao Li73860e32015-08-19 16:30:16 -0700314 }
315
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000316 return face;
Yanbiao Li73860e32015-08-19 16:30:16 -0700317}
318
319void
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700320FaceManager::setLinkServiceOptions(Face& face,
321 const ControlParameters& parameters,
322 ControlParameters& response)
323{
324 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
325 BOOST_ASSERT(linkService != nullptr);
326
327 auto options = linkService->getOptions();
328 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
329 face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
330 options.allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
331 }
332 linkService->setOptions(options);
333
334 // Set Flags for ControlResponse
335 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false);
336}
337
338void
Yanbiao Li73860e32015-08-19 16:30:16 -0700339FaceManager::listFaces(const Name& topPrefix, const Interest& interest,
340 ndn::mgmt::StatusDatasetContext& context)
341{
Eric Newberryc64d30a2015-12-26 11:07:27 -0700342 auto now = time::steady_clock::now();
Junxiao Shib84e6742016-07-19 13:16:22 +0000343 for (const Face& face : m_faceTable) {
344 ndn::nfd::FaceStatus status = collectFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700345 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700346 }
347 context.end();
348}
349
350void
351FaceManager::listChannels(const Name& topPrefix, const Interest& interest,
352 ndn::mgmt::StatusDatasetContext& context)
353{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200354 std::set<const ProtocolFactory*> seenFactories;
Yanbiao Li73860e32015-08-19 16:30:16 -0700355
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200356 for (const auto& kv : m_factories) {
357 const ProtocolFactory* factory = kv.second.get();
358 bool inserted;
359 std::tie(std::ignore, inserted) = seenFactories.insert(factory);
Yanbiao Li73860e32015-08-19 16:30:16 -0700360
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200361 if (inserted) {
362 for (const auto& channel : factory->getChannels()) {
363 ndn::nfd::ChannelStatus entry;
364 entry.setLocalUri(channel->getUri().toString());
365 context.append(entry.wireEncode());
366 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700367 }
368 }
369
370 context.end();
371}
372
373void
374FaceManager::queryFaces(const Name& topPrefix, const Interest& interest,
375 ndn::mgmt::StatusDatasetContext& context)
376{
377 ndn::nfd::FaceQueryFilter faceFilter;
378 const Name& query = interest.getName();
379 try {
380 faceFilter.wireDecode(query[-1].blockFromValue());
381 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200382 catch (const tlv::Error& e) {
383 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
384 return context.reject(ControlResponse(400, "Malformed filter"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700385 }
386
Eric Newberryc64d30a2015-12-26 11:07:27 -0700387 auto now = time::steady_clock::now();
Junxiao Shib84e6742016-07-19 13:16:22 +0000388 for (const Face& face : m_faceTable) {
389 if (!matchFilter(faceFilter, face)) {
Junxiao Shida93f1f2015-11-11 06:13:16 -0700390 continue;
Yanbiao Li73860e32015-08-19 16:30:16 -0700391 }
Junxiao Shib84e6742016-07-19 13:16:22 +0000392 ndn::nfd::FaceStatus status = collectFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700393 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700394 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200395
Yanbiao Li73860e32015-08-19 16:30:16 -0700396 context.end();
397}
398
399bool
Junxiao Shib84e6742016-07-19 13:16:22 +0000400FaceManager::matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700401{
402 if (filter.hasFaceId() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000403 filter.getFaceId() != static_cast<uint64_t>(face.getId())) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700404 return false;
405 }
406
407 if (filter.hasUriScheme() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000408 filter.getUriScheme() != face.getRemoteUri().getScheme() &&
409 filter.getUriScheme() != face.getLocalUri().getScheme()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700410 return false;
411 }
412
413 if (filter.hasRemoteUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000414 filter.getRemoteUri() != face.getRemoteUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700415 return false;
416 }
417
418 if (filter.hasLocalUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000419 filter.getLocalUri() != face.getLocalUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700420 return false;
421 }
422
423 if (filter.hasFaceScope() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000424 filter.getFaceScope() != face.getScope()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700425 return false;
426 }
427
428 if (filter.hasFacePersistency() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000429 filter.getFacePersistency() != face.getPersistency()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700430 return false;
431 }
432
433 if (filter.hasLinkType() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000434 filter.getLinkType() != face.getLinkType()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700435 return false;
436 }
437
438 return true;
439}
440
Eric Newberryc64d30a2015-12-26 11:07:27 -0700441ndn::nfd::FaceStatus
442FaceManager::collectFaceStatus(const Face& face, const time::steady_clock::TimePoint& now)
443{
444 ndn::nfd::FaceStatus status;
445
446 collectFaceProperties(face, status);
447
448 time::steady_clock::TimePoint expirationTime = face.getExpirationTime();
449 if (expirationTime != time::steady_clock::TimePoint::max()) {
450 status.setExpirationPeriod(std::max(time::milliseconds(0),
451 time::duration_cast<time::milliseconds>(expirationTime - now)));
452 }
453
454 const face::FaceCounters& counters = face.getCounters();
455 status.setNInInterests(counters.nInInterests)
456 .setNOutInterests(counters.nOutInterests)
457 .setNInDatas(counters.nInData)
458 .setNOutDatas(counters.nOutData)
459 .setNInNacks(counters.nInNacks)
460 .setNOutNacks(counters.nOutNacks)
461 .setNInBytes(counters.nInBytes)
462 .setNOutBytes(counters.nOutBytes);
463
464 return status;
465}
466
Junxiao Shida93f1f2015-11-11 06:13:16 -0700467template<typename FaceTraits>
Junxiao Shicde37ad2015-12-24 01:02:05 -0700468void
469FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits)
Junxiao Shida93f1f2015-11-11 06:13:16 -0700470{
471 traits.setFaceId(face.getId())
472 .setRemoteUri(face.getRemoteUri().toString())
473 .setLocalUri(face.getLocalUri().toString())
474 .setFaceScope(face.getScope())
475 .setFacePersistency(face.getPersistency())
476 .setLinkType(face.getLinkType());
Junxiao Shida93f1f2015-11-11 06:13:16 -0700477}
478
479void
Junxiao Shiae04d342016-07-19 13:20:22 +0000480FaceManager::notifyAddFace(const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700481{
482 ndn::nfd::FaceEventNotification notification;
483 notification.setKind(ndn::nfd::FACE_EVENT_CREATED);
Junxiao Shiae04d342016-07-19 13:20:22 +0000484 collectFaceProperties(face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700485
Junxiao Shiae04d342016-07-19 13:20:22 +0000486 m_postNotification(notification.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700487}
488
489void
Junxiao Shiae04d342016-07-19 13:20:22 +0000490FaceManager::notifyRemoveFace(const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700491{
492 ndn::nfd::FaceEventNotification notification;
493 notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED);
Junxiao Shiae04d342016-07-19 13:20:22 +0000494 collectFaceProperties(face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700495
Junxiao Shiae04d342016-07-19 13:20:22 +0000496 m_postNotification(notification.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700497}
498
499void
500FaceManager::processConfig(const ConfigSection& configSection,
501 bool isDryRun,
502 const std::string& filename)
503{
504 bool hasSeenUnix = false;
505 bool hasSeenTcp = false;
506 bool hasSeenUdp = false;
507 bool hasSeenEther = false;
508 bool hasSeenWebSocket = false;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200509 auto nicList = listNetworkInterfaces();
Yanbiao Li73860e32015-08-19 16:30:16 -0700510
511 for (const auto& item : configSection) {
512 if (item.first == "unix") {
513 if (hasSeenUnix) {
514 BOOST_THROW_EXCEPTION(Error("Duplicate \"unix\" section"));
515 }
516 hasSeenUnix = true;
517
518 processSectionUnix(item.second, isDryRun);
519 }
520 else if (item.first == "tcp") {
521 if (hasSeenTcp) {
522 BOOST_THROW_EXCEPTION(Error("Duplicate \"tcp\" section"));
523 }
524 hasSeenTcp = true;
525
526 processSectionTcp(item.second, isDryRun);
527 }
528 else if (item.first == "udp") {
529 if (hasSeenUdp) {
530 BOOST_THROW_EXCEPTION(Error("Duplicate \"udp\" section"));
531 }
532 hasSeenUdp = true;
533
534 processSectionUdp(item.second, isDryRun, nicList);
535 }
536 else if (item.first == "ether") {
537 if (hasSeenEther) {
538 BOOST_THROW_EXCEPTION(Error("Duplicate \"ether\" section"));
539 }
540 hasSeenEther = true;
541
542 processSectionEther(item.second, isDryRun, nicList);
543 }
544 else if (item.first == "websocket") {
545 if (hasSeenWebSocket) {
546 BOOST_THROW_EXCEPTION(Error("Duplicate \"websocket\" section"));
547 }
548 hasSeenWebSocket = true;
549
550 processSectionWebSocket(item.second, isDryRun);
551 }
552 else {
553 BOOST_THROW_EXCEPTION(Error("Unrecognized option \"" + item.first + "\""));
554 }
555 }
556}
557
558void
559FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun)
560{
561 // ; the unix section contains settings of Unix stream faces and channels
562 // unix
563 // {
564 // path /var/run/nfd.sock ; Unix stream listener path
565 // }
566
567#if defined(HAVE_UNIX_SOCKETS)
Yanbiao Li73860e32015-08-19 16:30:16 -0700568 std::string path = "/var/run/nfd.sock";
569
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200570 for (const auto& i : configSection) {
571 if (i.first == "path") {
572 path = i.second.get_value<std::string>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700573 }
574 else {
575 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200576 i.first + "\" in \"unix\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700577 }
578 }
579
580 if (!isDryRun) {
581 if (m_factories.count("unix") > 0) {
582 return;
583 }
584
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200585 auto factory = make_shared<UnixStreamFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700586 m_factories.insert(std::make_pair("unix", factory));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200587
588 auto channel = factory->createChannel(path);
589 channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700590 }
591#else
592 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, "
593 "cannot process \"unix\" section"));
594#endif // HAVE_UNIX_SOCKETS
595}
596
597void
598FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun)
599{
600 // ; the tcp section contains settings of TCP faces and channels
601 // tcp
602 // {
603 // listen yes ; set to 'no' to disable TCP listener, default 'yes'
604 // port 6363 ; TCP listener port number
605 // }
606
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200607 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700608 bool needToListen = true;
609 bool enableV4 = true;
610 bool enableV6 = true;
611
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200612 for (const auto& i : configSection) {
613 if (i.first == "port") {
614 port = ConfigFile::parseNumber<uint16_t>(i, "tcp");
615 NFD_LOG_TRACE("TCP port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700616 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200617 else if (i.first == "listen") {
618 needToListen = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700619 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200620 else if (i.first == "enable_v4") {
621 enableV4 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700622 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200623 else if (i.first == "enable_v6") {
624 enableV6 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700625 }
626 else {
627 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200628 i.first + "\" in \"tcp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700629 }
630 }
631
632 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200633 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 TCP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700634 " Remove \"tcp\" section to disable TCP channels or"
635 " re-enable at least one channel type."));
636 }
637
638 if (!isDryRun) {
639 if (m_factories.count("tcp") > 0) {
640 return;
641 }
642
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200643 auto factory = make_shared<TcpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700644 m_factories.insert(std::make_pair("tcp", factory));
645
646 if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200647 tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port);
648 shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700649 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200650 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700651 }
652
653 m_factories.insert(std::make_pair("tcp4", factory));
654 }
655
656 if (enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200657 tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port);
658 shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700659 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200660 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700661 }
662
663 m_factories.insert(std::make_pair("tcp6", factory));
664 }
665 }
666}
667
668void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200669FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700670 const std::vector<NetworkInterfaceInfo>& nicList)
671{
672 // ; the udp section contains settings of UDP faces and channels
673 // udp
674 // {
675 // port 6363 ; UDP unicast port number
676 // idle_timeout 600 ; idle time (seconds) before closing a UDP unicast face
677 // keep_alive_interval 25 ; interval (seconds) between keep-alive refreshes
678
679 // ; NFD creates one UDP multicast face per NIC
680 // mcast yes ; set to 'no' to disable UDP multicast, default 'yes'
681 // mcast_port 56363 ; UDP multicast port number
682 // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only)
683 // }
684
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200685 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700686 bool enableV4 = true;
687 bool enableV6 = true;
688 size_t timeout = 600;
689 size_t keepAliveInterval = 25;
690 bool useMcast = true;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200691 auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170");
692 uint16_t mcastPort = 56363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700693
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200694 for (const auto& i : configSection) {
695 if (i.first == "port") {
696 port = ConfigFile::parseNumber<uint16_t>(i, "udp");
697 NFD_LOG_TRACE("UDP unicast port set to " << port);
698 }
699 else if (i.first == "enable_v4") {
700 enableV4 = ConfigFile::parseYesNo(i, "udp");
701 }
702 else if (i.first == "enable_v6") {
703 enableV6 = ConfigFile::parseYesNo(i, "udp");
704 }
705 else if (i.first == "idle_timeout") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700706 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200707 timeout = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700708 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200709 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700710 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200711 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700712 }
713 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200714 else if (i.first == "keep_alive_interval") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700715 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200716 keepAliveInterval = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700717 /// \todo Make use of keepAliveInterval
Yanbiao Li73860e32015-08-19 16:30:16 -0700718 (void)(keepAliveInterval);
719 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200720 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700721 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200722 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700723 }
724 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200725 else if (i.first == "mcast") {
726 useMcast = ConfigFile::parseYesNo(i, "udp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700727 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200728 else if (i.first == "mcast_port") {
729 mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp");
730 NFD_LOG_TRACE("UDP multicast port set to " << mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700731 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200732 else if (i.first == "mcast_group") {
733 boost::system::error_code ec;
734 mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec);
735 if (ec) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700736 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200737 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700738 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200739 NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700740 }
741 else {
742 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200743 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700744 }
745 }
746
747 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200748 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 UDP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700749 " Remove \"udp\" section to disable UDP channels or"
750 " re-enable at least one channel type."));
751 }
752 else if (useMcast && !enableV4) {
753 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 multicast requested, but IPv4 channels"
754 " have been disabled (conflicting configuration options set)"));
755 }
756
757 if (!isDryRun) {
758 shared_ptr<UdpFactory> factory;
759 bool isReload = false;
760 if (m_factories.count("udp") > 0) {
761 isReload = true;
762 factory = static_pointer_cast<UdpFactory>(m_factories["udp"]);
763 }
764 else {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200765 factory = make_shared<UdpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700766 m_factories.insert(std::make_pair("udp", factory));
767 }
768
769 if (!isReload && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200770 udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port);
771 shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout));
772 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700773
774 m_factories.insert(std::make_pair("udp4", factory));
775 }
776
777 if (!isReload && enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200778 udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port);
779 shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout));
780 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700781
Yanbiao Li73860e32015-08-19 16:30:16 -0700782 m_factories.insert(std::make_pair("udp6", factory));
783 }
784
Junxiao Shicde37ad2015-12-24 01:02:05 -0700785 std::set<shared_ptr<Face>> multicastFacesToRemove;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200786 for (const auto& i : factory->getMulticastFaces()) {
787 multicastFacesToRemove.insert(i.second);
788 }
789
Yanbiao Li73860e32015-08-19 16:30:16 -0700790 if (useMcast && enableV4) {
791 std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces;
792 for (const auto& nic : nicList) {
793 if (nic.isUp() && nic.isMulticastCapable() && !nic.ipv4Addresses.empty()) {
794 ipv4MulticastInterfaces.push_back(nic);
795 }
796 }
797
798 bool isNicNameNecessary = false;
799#if defined(__linux__)
800 if (ipv4MulticastInterfaces.size() > 1) {
801 // On Linux if we have more than one MulticastUdpFace
802 // we need to specify the name of the interface
803 isNicNameNecessary = true;
804 }
805#endif
806
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200807 udp::Endpoint mcastEndpoint(mcastGroup, mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700808 for (const auto& nic : ipv4MulticastInterfaces) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200809 udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort);
810 auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint,
Yukai Tu0a49d342015-09-13 12:54:22 +0800811 isNicNameNecessary ? nic.name : "");
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200812 m_faceTable.add(newFace);
813 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700814 }
815 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700816
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200817 for (const auto& face : multicastFacesToRemove) {
818 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700819 }
820 }
821}
822
823void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200824FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700825 const std::vector<NetworkInterfaceInfo>& nicList)
826{
827 // ; the ether section contains settings of Ethernet faces and channels
828 // ether
829 // {
830 // ; NFD creates one Ethernet multicast face per NIC
831 // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes'
832 // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group
833 // }
834
835#if defined(HAVE_LIBPCAP)
836 bool useMcast = true;
837 ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress());
838
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200839 for (const auto& i : configSection) {
840 if (i.first == "mcast") {
841 useMcast = ConfigFile::parseYesNo(i, "ether");
Yanbiao Li73860e32015-08-19 16:30:16 -0700842 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200843 else if (i.first == "mcast_group") {
844 mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>());
Yanbiao Li73860e32015-08-19 16:30:16 -0700845 if (mcastGroup.isNull()) {
846 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200847 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700848 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200849 NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700850 }
851 else {
852 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200853 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700854 }
855 }
856
857 if (!isDryRun) {
858 shared_ptr<EthernetFactory> factory;
859 if (m_factories.count("ether") > 0) {
860 factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]);
861 }
862 else {
863 factory = make_shared<EthernetFactory>();
864 m_factories.insert(std::make_pair("ether", factory));
865 }
866
Junxiao Shicde37ad2015-12-24 01:02:05 -0700867 std::set<shared_ptr<Face>> multicastFacesToRemove;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200868 for (const auto& i : factory->getMulticastFaces()) {
869 multicastFacesToRemove.insert(i.second);
870 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700871
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200872 if (useMcast) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700873 for (const auto& nic : nicList) {
874 if (nic.isUp() && nic.isMulticastCapable()) {
875 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200876 auto newFace = factory->createMulticastFace(nic, mcastGroup);
877 m_faceTable.add(newFace);
878 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700879 }
880 catch (const EthernetFactory::Error& factoryError) {
881 NFD_LOG_ERROR(factoryError.what() << ", continuing");
882 }
Davide Pesavento35120ea2015-11-17 21:13:18 +0100883 catch (const face::EthernetTransport::Error& faceError) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700884 NFD_LOG_ERROR(faceError.what() << ", continuing");
885 }
886 }
887 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700888 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700889
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200890 for (const auto& face : multicastFacesToRemove) {
891 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700892 }
893 }
894#else
895 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section"));
896#endif // HAVE_LIBPCAP
897}
898
899void
900FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun)
901{
902 // ; the websocket section contains settings of WebSocket faces and channels
903 // websocket
904 // {
905 // listen yes ; set to 'no' to disable WebSocket listener, default 'yes'
906 // port 9696 ; WebSocket listener port number
907 // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes'
908 // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes'
909 // }
910
911#if defined(HAVE_WEBSOCKET)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200912 uint16_t port = 9696;
Yanbiao Li73860e32015-08-19 16:30:16 -0700913 bool needToListen = true;
914 bool enableV4 = true;
915 bool enableV6 = true;
916
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200917 for (const auto& i : configSection) {
918 if (i.first == "port") {
919 port = ConfigFile::parseNumber<uint16_t>(i, "websocket");
920 NFD_LOG_TRACE("WebSocket port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700921 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200922 else if (i.first == "listen") {
923 needToListen = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700924 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200925 else if (i.first == "enable_v4") {
926 enableV4 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700927 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200928 else if (i.first == "enable_v6") {
929 enableV6 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700930 }
931 else {
932 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200933 i.first + "\" in \"websocket\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700934 }
935 }
936
937 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200938 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 WebSocket channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700939 " Remove \"websocket\" section to disable WebSocket channels or"
940 " re-enable at least one channel type."));
941 }
942
943 if (!enableV4 && enableV6) {
944 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel."));
945 }
946
947 if (!isDryRun) {
948 if (m_factories.count("websocket") > 0) {
949 return;
950 }
951
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200952 auto factory = make_shared<WebSocketFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700953 m_factories.insert(std::make_pair("websocket", factory));
954
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200955 shared_ptr<WebSocketChannel> channel;
956
Yanbiao Li73860e32015-08-19 16:30:16 -0700957 if (enableV6 && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200958 websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port);
959 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700960
961 m_factories.insert(std::make_pair("websocket46", factory));
962 }
963 else if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200964 websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port);
965 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700966
967 m_factories.insert(std::make_pair("websocket4", factory));
968 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200969
970 if (channel && needToListen) {
971 channel->listen(bind(&FaceTable::add, &m_faceTable, _1));
972 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700973 }
974#else
975 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, "
976 "cannot process \"websocket\" section"));
977#endif // HAVE_WEBSOCKET
978}
979
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200980} // namespace nfd