blob: 9cd0e07f530a796f86ff93b081325a1605cc6498 [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
62 registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy",
63 bind(&FaceManager::destroyFace, this, _2, _3, _4, _5));
64
65 registerCommandHandler<ndn::nfd::FaceEnableLocalControlCommand>("enable-local-control",
66 bind(&FaceManager::enableLocalControl, this, _2, _3, _4, _5));
67
68 registerCommandHandler<ndn::nfd::FaceDisableLocalControlCommand>("disable-local-control",
69 bind(&FaceManager::disableLocalControl, this, _2, _3, _4, _5));
70
71 registerStatusDatasetHandler("list", bind(&FaceManager::listFaces, this, _1, _2, _3));
72 registerStatusDatasetHandler("channels", bind(&FaceManager::listChannels, this, _1, _2, _3));
73 registerStatusDatasetHandler("query", bind(&FaceManager::queryFaces, this, _1, _2, _3));
74
Junxiao Shiae04d342016-07-19 13:20:22 +000075 m_postNotification = registerNotificationStream("events");
76 m_faceAddConn = m_faceTable.afterAdd.connect(bind(&FaceManager::notifyAddFace, this, _1));
77 m_faceRemoveConn = m_faceTable.beforeRemove.connect(bind(&FaceManager::notifyRemoveFace, this, _1));
Yanbiao Li73860e32015-08-19 16:30:16 -070078}
79
80void
81FaceManager::setConfigFile(ConfigFile& configFile)
82{
83 configFile.addSectionHandler("face_system", bind(&FaceManager::processConfig, this, _1, _2, _3));
84}
85
86void
87FaceManager::createFace(const Name& topPrefix, const Interest& interest,
88 const ControlParameters& parameters,
89 const ndn::mgmt::CommandContinuation& done)
90{
91 FaceUri uri;
92 if (!uri.parse(parameters.getUri())) {
93 NFD_LOG_TRACE("failed to parse URI");
Eric Newberry42602412016-08-27 09:33:18 -070094 done(ControlResponse(400, "Malformed command"));
95 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070096 }
97
98 if (!uri.isCanonical()) {
99 NFD_LOG_TRACE("received non-canonical URI");
Eric Newberry42602412016-08-27 09:33:18 -0700100 done(ControlResponse(400, "Non-canonical URI"));
101 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700102 }
103
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200104 auto factory = m_factories.find(uri.getScheme());
Yanbiao Li73860e32015-08-19 16:30:16 -0700105 if (factory == m_factories.end()) {
Eric Newberry42602412016-08-27 09:33:18 -0700106 NFD_LOG_TRACE("received create request for unsupported protocol");
107 done(ControlResponse(406, "Unsupported protocol"));
108 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700109 }
110
111 try {
112 factory->second->createFace(uri,
113 parameters.getFacePersistency(),
114 bind(&FaceManager::afterCreateFaceSuccess,
115 this, parameters, _1, done),
116 bind(&FaceManager::afterCreateFaceFailure,
Eric Newberry42602412016-08-27 09:33:18 -0700117 this, _1, _2, done));
Yanbiao Li73860e32015-08-19 16:30:16 -0700118 }
119 catch (const std::runtime_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700120 NFD_LOG_ERROR("Face creation failed: " << error.what());
121 done(ControlResponse(500, "Face creation failed due to internal error"));
122 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700123 }
124 catch (const std::logic_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700125 NFD_LOG_ERROR("Face creation failed: " << error.what());
126 done(ControlResponse(500, "Face creation failed due to internal error"));
127 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700128 }
129}
130
Eric Newberry42602412016-08-27 09:33:18 -0700131/**
132 * \todo #3232
133 * If the creation of this face would conflict with an existing face (e.g. same underlying
134 * protocol and remote address, or a NIC-associated permanent face), the command will fail
135 * with StatusCode 409.
136 */
Yanbiao Li73860e32015-08-19 16:30:16 -0700137void
Eric Newberry42602412016-08-27 09:33:18 -0700138FaceManager::afterCreateFaceSuccess(const ControlParameters& parameters,
Yanbiao Li73860e32015-08-19 16:30:16 -0700139 const shared_ptr<Face>& newFace,
140 const ndn::mgmt::CommandContinuation& done)
141{
Eric Newberry42602412016-08-27 09:33:18 -0700142 ControlParameters response;
Yanbiao Li73860e32015-08-19 16:30:16 -0700143
Eric Newberry42602412016-08-27 09:33:18 -0700144 m_faceTable.add(newFace);
145
146 // Set ControlResponse parameters
147 response.setFaceId(newFace->getId());
148 response.setFacePersistency(newFace->getPersistency());
149
150 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
151}
152
153void
154FaceManager::afterCreateFaceFailure(uint32_t status,
155 const std::string& reason,
156 const ndn::mgmt::CommandContinuation& done)
157{
158 NFD_LOG_DEBUG("Face creation failed: " << reason);
159
160 done(ControlResponse(status, reason));
Yanbiao Li73860e32015-08-19 16:30:16 -0700161}
162
163void
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700164FaceManager::destroyFace(const Name& topPrefix, const Interest& interest,
165 const ControlParameters& parameters,
166 const ndn::mgmt::CommandContinuation& done)
167{
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000168 Face* face = m_faceTable.get(parameters.getFaceId());
169 if (face != nullptr) {
170 face->close();
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700171 }
172
173 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
174}
175
176void
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700177FaceManager::enableLocalControl(const Name& topPrefix, const Interest& interest,
178 const ControlParameters& parameters,
179 const ndn::mgmt::CommandContinuation& done)
180{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700181 Face* face = findFaceForLocalControl(interest, parameters, done);
182 if (!face) {
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700183 return;
184 }
185
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700186 // TODO#3226 redesign enable-local-control
187 // For now, enable-local-control will enable all local fields in GenericLinkService.
Junxiao Shicde37ad2015-12-24 01:02:05 -0700188 auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService());
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700189 if (service == nullptr) {
190 return done(ControlResponse(503, "LinkService type not supported"));
191 }
192
193 face::GenericLinkService::Options options = service->getOptions();
194 options.allowLocalFields = true;
195 service->setOptions(options);
196
197 return done(ControlResponse(200, "OK: enable all local fields on GenericLinkService")
198 .setBody(parameters.wireEncode()));
199}
200
201void
202FaceManager::disableLocalControl(const Name& topPrefix, const Interest& interest,
203 const ControlParameters& parameters,
204 const ndn::mgmt::CommandContinuation& done)
205{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700206 Face* face = findFaceForLocalControl(interest, parameters, done);
207 if (!face) {
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700208 return;
209 }
210
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700211 // TODO#3226 redesign disable-local-control
212 // For now, disable-local-control will disable all local fields in GenericLinkService.
Junxiao Shicde37ad2015-12-24 01:02:05 -0700213 auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService());
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700214 if (service == nullptr) {
215 return done(ControlResponse(503, "LinkService type not supported"));
216 }
217
218 face::GenericLinkService::Options options = service->getOptions();
219 options.allowLocalFields = false;
220 service->setOptions(options);
221
222 return done(ControlResponse(200, "OK: disable all local fields on GenericLinkService")
223 .setBody(parameters.wireEncode()));
224}
225
Junxiao Shicde37ad2015-12-24 01:02:05 -0700226Face*
227FaceManager::findFaceForLocalControl(const Interest& request,
228 const ControlParameters& parameters,
229 const ndn::mgmt::CommandContinuation& done)
Yanbiao Li73860e32015-08-19 16:30:16 -0700230{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000231 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
232 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
233 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
234 // and is initialized synchronously with IncomingFaceId field enabled.
235 BOOST_ASSERT(incomingFaceIdTag != nullptr);
236
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000237 Face* face = m_faceTable.get(*incomingFaceIdTag);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200238 if (face == nullptr) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000239 NFD_LOG_DEBUG("FaceId " << *incomingFaceIdTag << " not found");
Yanbiao Li73860e32015-08-19 16:30:16 -0700240 done(ControlResponse(410, "Face not found"));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700241 return nullptr;
Yanbiao Li73860e32015-08-19 16:30:16 -0700242 }
243
Junxiao Shicde37ad2015-12-24 01:02:05 -0700244 if (face->getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200245 NFD_LOG_DEBUG("Cannot enable local control on non-local FaceId " << face->getId());
Yanbiao Li73860e32015-08-19 16:30:16 -0700246 done(ControlResponse(412, "Face is non-local"));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700247 return nullptr;
Yanbiao Li73860e32015-08-19 16:30:16 -0700248 }
249
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000250 return face;
Yanbiao Li73860e32015-08-19 16:30:16 -0700251}
252
253void
254FaceManager::listFaces(const Name& topPrefix, const Interest& interest,
255 ndn::mgmt::StatusDatasetContext& context)
256{
Eric Newberryc64d30a2015-12-26 11:07:27 -0700257 auto now = time::steady_clock::now();
Junxiao Shib84e6742016-07-19 13:16:22 +0000258 for (const Face& face : m_faceTable) {
259 ndn::nfd::FaceStatus status = collectFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700260 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700261 }
262 context.end();
263}
264
265void
266FaceManager::listChannels(const Name& topPrefix, const Interest& interest,
267 ndn::mgmt::StatusDatasetContext& context)
268{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200269 std::set<const ProtocolFactory*> seenFactories;
Yanbiao Li73860e32015-08-19 16:30:16 -0700270
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200271 for (const auto& kv : m_factories) {
272 const ProtocolFactory* factory = kv.second.get();
273 bool inserted;
274 std::tie(std::ignore, inserted) = seenFactories.insert(factory);
Yanbiao Li73860e32015-08-19 16:30:16 -0700275
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200276 if (inserted) {
277 for (const auto& channel : factory->getChannels()) {
278 ndn::nfd::ChannelStatus entry;
279 entry.setLocalUri(channel->getUri().toString());
280 context.append(entry.wireEncode());
281 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700282 }
283 }
284
285 context.end();
286}
287
288void
289FaceManager::queryFaces(const Name& topPrefix, const Interest& interest,
290 ndn::mgmt::StatusDatasetContext& context)
291{
292 ndn::nfd::FaceQueryFilter faceFilter;
293 const Name& query = interest.getName();
294 try {
295 faceFilter.wireDecode(query[-1].blockFromValue());
296 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200297 catch (const tlv::Error& e) {
298 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
299 return context.reject(ControlResponse(400, "Malformed filter"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700300 }
301
Eric Newberryc64d30a2015-12-26 11:07:27 -0700302 auto now = time::steady_clock::now();
Junxiao Shib84e6742016-07-19 13:16:22 +0000303 for (const Face& face : m_faceTable) {
304 if (!matchFilter(faceFilter, face)) {
Junxiao Shida93f1f2015-11-11 06:13:16 -0700305 continue;
Yanbiao Li73860e32015-08-19 16:30:16 -0700306 }
Junxiao Shib84e6742016-07-19 13:16:22 +0000307 ndn::nfd::FaceStatus status = collectFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700308 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700309 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200310
Yanbiao Li73860e32015-08-19 16:30:16 -0700311 context.end();
312}
313
314bool
Junxiao Shib84e6742016-07-19 13:16:22 +0000315FaceManager::matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700316{
317 if (filter.hasFaceId() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000318 filter.getFaceId() != static_cast<uint64_t>(face.getId())) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700319 return false;
320 }
321
322 if (filter.hasUriScheme() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000323 filter.getUriScheme() != face.getRemoteUri().getScheme() &&
324 filter.getUriScheme() != face.getLocalUri().getScheme()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700325 return false;
326 }
327
328 if (filter.hasRemoteUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000329 filter.getRemoteUri() != face.getRemoteUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700330 return false;
331 }
332
333 if (filter.hasLocalUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000334 filter.getLocalUri() != face.getLocalUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700335 return false;
336 }
337
338 if (filter.hasFaceScope() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000339 filter.getFaceScope() != face.getScope()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700340 return false;
341 }
342
343 if (filter.hasFacePersistency() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000344 filter.getFacePersistency() != face.getPersistency()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700345 return false;
346 }
347
348 if (filter.hasLinkType() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000349 filter.getLinkType() != face.getLinkType()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700350 return false;
351 }
352
353 return true;
354}
355
Eric Newberryc64d30a2015-12-26 11:07:27 -0700356ndn::nfd::FaceStatus
357FaceManager::collectFaceStatus(const Face& face, const time::steady_clock::TimePoint& now)
358{
359 ndn::nfd::FaceStatus status;
360
361 collectFaceProperties(face, status);
362
363 time::steady_clock::TimePoint expirationTime = face.getExpirationTime();
364 if (expirationTime != time::steady_clock::TimePoint::max()) {
365 status.setExpirationPeriod(std::max(time::milliseconds(0),
366 time::duration_cast<time::milliseconds>(expirationTime - now)));
367 }
368
369 const face::FaceCounters& counters = face.getCounters();
370 status.setNInInterests(counters.nInInterests)
371 .setNOutInterests(counters.nOutInterests)
372 .setNInDatas(counters.nInData)
373 .setNOutDatas(counters.nOutData)
374 .setNInNacks(counters.nInNacks)
375 .setNOutNacks(counters.nOutNacks)
376 .setNInBytes(counters.nInBytes)
377 .setNOutBytes(counters.nOutBytes);
378
379 return status;
380}
381
Junxiao Shida93f1f2015-11-11 06:13:16 -0700382template<typename FaceTraits>
Junxiao Shicde37ad2015-12-24 01:02:05 -0700383void
384FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits)
Junxiao Shida93f1f2015-11-11 06:13:16 -0700385{
386 traits.setFaceId(face.getId())
387 .setRemoteUri(face.getRemoteUri().toString())
388 .setLocalUri(face.getLocalUri().toString())
389 .setFaceScope(face.getScope())
390 .setFacePersistency(face.getPersistency())
391 .setLinkType(face.getLinkType());
Junxiao Shida93f1f2015-11-11 06:13:16 -0700392}
393
394void
Junxiao Shiae04d342016-07-19 13:20:22 +0000395FaceManager::notifyAddFace(const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700396{
397 ndn::nfd::FaceEventNotification notification;
398 notification.setKind(ndn::nfd::FACE_EVENT_CREATED);
Junxiao Shiae04d342016-07-19 13:20:22 +0000399 collectFaceProperties(face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700400
Junxiao Shiae04d342016-07-19 13:20:22 +0000401 m_postNotification(notification.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700402}
403
404void
Junxiao Shiae04d342016-07-19 13:20:22 +0000405FaceManager::notifyRemoveFace(const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700406{
407 ndn::nfd::FaceEventNotification notification;
408 notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED);
Junxiao Shiae04d342016-07-19 13:20:22 +0000409 collectFaceProperties(face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700410
Junxiao Shiae04d342016-07-19 13:20:22 +0000411 m_postNotification(notification.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700412}
413
414void
415FaceManager::processConfig(const ConfigSection& configSection,
416 bool isDryRun,
417 const std::string& filename)
418{
419 bool hasSeenUnix = false;
420 bool hasSeenTcp = false;
421 bool hasSeenUdp = false;
422 bool hasSeenEther = false;
423 bool hasSeenWebSocket = false;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200424 auto nicList = listNetworkInterfaces();
Yanbiao Li73860e32015-08-19 16:30:16 -0700425
426 for (const auto& item : configSection) {
427 if (item.first == "unix") {
428 if (hasSeenUnix) {
429 BOOST_THROW_EXCEPTION(Error("Duplicate \"unix\" section"));
430 }
431 hasSeenUnix = true;
432
433 processSectionUnix(item.second, isDryRun);
434 }
435 else if (item.first == "tcp") {
436 if (hasSeenTcp) {
437 BOOST_THROW_EXCEPTION(Error("Duplicate \"tcp\" section"));
438 }
439 hasSeenTcp = true;
440
441 processSectionTcp(item.second, isDryRun);
442 }
443 else if (item.first == "udp") {
444 if (hasSeenUdp) {
445 BOOST_THROW_EXCEPTION(Error("Duplicate \"udp\" section"));
446 }
447 hasSeenUdp = true;
448
449 processSectionUdp(item.second, isDryRun, nicList);
450 }
451 else if (item.first == "ether") {
452 if (hasSeenEther) {
453 BOOST_THROW_EXCEPTION(Error("Duplicate \"ether\" section"));
454 }
455 hasSeenEther = true;
456
457 processSectionEther(item.second, isDryRun, nicList);
458 }
459 else if (item.first == "websocket") {
460 if (hasSeenWebSocket) {
461 BOOST_THROW_EXCEPTION(Error("Duplicate \"websocket\" section"));
462 }
463 hasSeenWebSocket = true;
464
465 processSectionWebSocket(item.second, isDryRun);
466 }
467 else {
468 BOOST_THROW_EXCEPTION(Error("Unrecognized option \"" + item.first + "\""));
469 }
470 }
471}
472
473void
474FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun)
475{
476 // ; the unix section contains settings of Unix stream faces and channels
477 // unix
478 // {
479 // path /var/run/nfd.sock ; Unix stream listener path
480 // }
481
482#if defined(HAVE_UNIX_SOCKETS)
Yanbiao Li73860e32015-08-19 16:30:16 -0700483 std::string path = "/var/run/nfd.sock";
484
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200485 for (const auto& i : configSection) {
486 if (i.first == "path") {
487 path = i.second.get_value<std::string>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700488 }
489 else {
490 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200491 i.first + "\" in \"unix\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700492 }
493 }
494
495 if (!isDryRun) {
496 if (m_factories.count("unix") > 0) {
497 return;
498 }
499
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200500 auto factory = make_shared<UnixStreamFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700501 m_factories.insert(std::make_pair("unix", factory));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200502
503 auto channel = factory->createChannel(path);
504 channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700505 }
506#else
507 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, "
508 "cannot process \"unix\" section"));
509#endif // HAVE_UNIX_SOCKETS
510}
511
512void
513FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun)
514{
515 // ; the tcp section contains settings of TCP faces and channels
516 // tcp
517 // {
518 // listen yes ; set to 'no' to disable TCP listener, default 'yes'
519 // port 6363 ; TCP listener port number
520 // }
521
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200522 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700523 bool needToListen = true;
524 bool enableV4 = true;
525 bool enableV6 = true;
526
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200527 for (const auto& i : configSection) {
528 if (i.first == "port") {
529 port = ConfigFile::parseNumber<uint16_t>(i, "tcp");
530 NFD_LOG_TRACE("TCP port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700531 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200532 else if (i.first == "listen") {
533 needToListen = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700534 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200535 else if (i.first == "enable_v4") {
536 enableV4 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700537 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200538 else if (i.first == "enable_v6") {
539 enableV6 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700540 }
541 else {
542 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200543 i.first + "\" in \"tcp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700544 }
545 }
546
547 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200548 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 TCP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700549 " Remove \"tcp\" section to disable TCP channels or"
550 " re-enable at least one channel type."));
551 }
552
553 if (!isDryRun) {
554 if (m_factories.count("tcp") > 0) {
555 return;
556 }
557
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200558 auto factory = make_shared<TcpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700559 m_factories.insert(std::make_pair("tcp", factory));
560
561 if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200562 tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port);
563 shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700564 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200565 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700566 }
567
568 m_factories.insert(std::make_pair("tcp4", factory));
569 }
570
571 if (enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200572 tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port);
573 shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700574 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200575 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700576 }
577
578 m_factories.insert(std::make_pair("tcp6", factory));
579 }
580 }
581}
582
583void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200584FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700585 const std::vector<NetworkInterfaceInfo>& nicList)
586{
587 // ; the udp section contains settings of UDP faces and channels
588 // udp
589 // {
590 // port 6363 ; UDP unicast port number
591 // idle_timeout 600 ; idle time (seconds) before closing a UDP unicast face
592 // keep_alive_interval 25 ; interval (seconds) between keep-alive refreshes
593
594 // ; NFD creates one UDP multicast face per NIC
595 // mcast yes ; set to 'no' to disable UDP multicast, default 'yes'
596 // mcast_port 56363 ; UDP multicast port number
597 // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only)
598 // }
599
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200600 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700601 bool enableV4 = true;
602 bool enableV6 = true;
603 size_t timeout = 600;
604 size_t keepAliveInterval = 25;
605 bool useMcast = true;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200606 auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170");
607 uint16_t mcastPort = 56363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700608
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200609 for (const auto& i : configSection) {
610 if (i.first == "port") {
611 port = ConfigFile::parseNumber<uint16_t>(i, "udp");
612 NFD_LOG_TRACE("UDP unicast port set to " << port);
613 }
614 else if (i.first == "enable_v4") {
615 enableV4 = ConfigFile::parseYesNo(i, "udp");
616 }
617 else if (i.first == "enable_v6") {
618 enableV6 = ConfigFile::parseYesNo(i, "udp");
619 }
620 else if (i.first == "idle_timeout") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700621 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200622 timeout = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700623 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200624 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700625 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200626 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700627 }
628 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200629 else if (i.first == "keep_alive_interval") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700630 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200631 keepAliveInterval = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700632 /// \todo Make use of keepAliveInterval
Yanbiao Li73860e32015-08-19 16:30:16 -0700633 (void)(keepAliveInterval);
634 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200635 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700636 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200637 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700638 }
639 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200640 else if (i.first == "mcast") {
641 useMcast = ConfigFile::parseYesNo(i, "udp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700642 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200643 else if (i.first == "mcast_port") {
644 mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp");
645 NFD_LOG_TRACE("UDP multicast port set to " << mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700646 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200647 else if (i.first == "mcast_group") {
648 boost::system::error_code ec;
649 mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec);
650 if (ec) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700651 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200652 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700653 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200654 NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700655 }
656 else {
657 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200658 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700659 }
660 }
661
662 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200663 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 UDP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700664 " Remove \"udp\" section to disable UDP channels or"
665 " re-enable at least one channel type."));
666 }
667 else if (useMcast && !enableV4) {
668 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 multicast requested, but IPv4 channels"
669 " have been disabled (conflicting configuration options set)"));
670 }
671
672 if (!isDryRun) {
673 shared_ptr<UdpFactory> factory;
674 bool isReload = false;
675 if (m_factories.count("udp") > 0) {
676 isReload = true;
677 factory = static_pointer_cast<UdpFactory>(m_factories["udp"]);
678 }
679 else {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200680 factory = make_shared<UdpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700681 m_factories.insert(std::make_pair("udp", factory));
682 }
683
684 if (!isReload && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200685 udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port);
686 shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout));
687 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700688
689 m_factories.insert(std::make_pair("udp4", factory));
690 }
691
692 if (!isReload && enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200693 udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port);
694 shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout));
695 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700696
Yanbiao Li73860e32015-08-19 16:30:16 -0700697 m_factories.insert(std::make_pair("udp6", factory));
698 }
699
Junxiao Shicde37ad2015-12-24 01:02:05 -0700700 std::set<shared_ptr<Face>> multicastFacesToRemove;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200701 for (const auto& i : factory->getMulticastFaces()) {
702 multicastFacesToRemove.insert(i.second);
703 }
704
Yanbiao Li73860e32015-08-19 16:30:16 -0700705 if (useMcast && enableV4) {
706 std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces;
707 for (const auto& nic : nicList) {
708 if (nic.isUp() && nic.isMulticastCapable() && !nic.ipv4Addresses.empty()) {
709 ipv4MulticastInterfaces.push_back(nic);
710 }
711 }
712
713 bool isNicNameNecessary = false;
714#if defined(__linux__)
715 if (ipv4MulticastInterfaces.size() > 1) {
716 // On Linux if we have more than one MulticastUdpFace
717 // we need to specify the name of the interface
718 isNicNameNecessary = true;
719 }
720#endif
721
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200722 udp::Endpoint mcastEndpoint(mcastGroup, mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700723 for (const auto& nic : ipv4MulticastInterfaces) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200724 udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort);
725 auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint,
Yukai Tu0a49d342015-09-13 12:54:22 +0800726 isNicNameNecessary ? nic.name : "");
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200727 m_faceTable.add(newFace);
728 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700729 }
730 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700731
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200732 for (const auto& face : multicastFacesToRemove) {
733 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700734 }
735 }
736}
737
738void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200739FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700740 const std::vector<NetworkInterfaceInfo>& nicList)
741{
742 // ; the ether section contains settings of Ethernet faces and channels
743 // ether
744 // {
745 // ; NFD creates one Ethernet multicast face per NIC
746 // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes'
747 // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group
748 // }
749
750#if defined(HAVE_LIBPCAP)
751 bool useMcast = true;
752 ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress());
753
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200754 for (const auto& i : configSection) {
755 if (i.first == "mcast") {
756 useMcast = ConfigFile::parseYesNo(i, "ether");
Yanbiao Li73860e32015-08-19 16:30:16 -0700757 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200758 else if (i.first == "mcast_group") {
759 mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>());
Yanbiao Li73860e32015-08-19 16:30:16 -0700760 if (mcastGroup.isNull()) {
761 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200762 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700763 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200764 NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700765 }
766 else {
767 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200768 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700769 }
770 }
771
772 if (!isDryRun) {
773 shared_ptr<EthernetFactory> factory;
774 if (m_factories.count("ether") > 0) {
775 factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]);
776 }
777 else {
778 factory = make_shared<EthernetFactory>();
779 m_factories.insert(std::make_pair("ether", factory));
780 }
781
Junxiao Shicde37ad2015-12-24 01:02:05 -0700782 std::set<shared_ptr<Face>> multicastFacesToRemove;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200783 for (const auto& i : factory->getMulticastFaces()) {
784 multicastFacesToRemove.insert(i.second);
785 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700786
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200787 if (useMcast) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700788 for (const auto& nic : nicList) {
789 if (nic.isUp() && nic.isMulticastCapable()) {
790 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200791 auto newFace = factory->createMulticastFace(nic, mcastGroup);
792 m_faceTable.add(newFace);
793 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700794 }
795 catch (const EthernetFactory::Error& factoryError) {
796 NFD_LOG_ERROR(factoryError.what() << ", continuing");
797 }
Davide Pesavento35120ea2015-11-17 21:13:18 +0100798 catch (const face::EthernetTransport::Error& faceError) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700799 NFD_LOG_ERROR(faceError.what() << ", continuing");
800 }
801 }
802 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700803 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700804
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200805 for (const auto& face : multicastFacesToRemove) {
806 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700807 }
808 }
809#else
810 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section"));
811#endif // HAVE_LIBPCAP
812}
813
814void
815FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun)
816{
817 // ; the websocket section contains settings of WebSocket faces and channels
818 // websocket
819 // {
820 // listen yes ; set to 'no' to disable WebSocket listener, default 'yes'
821 // port 9696 ; WebSocket listener port number
822 // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes'
823 // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes'
824 // }
825
826#if defined(HAVE_WEBSOCKET)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200827 uint16_t port = 9696;
Yanbiao Li73860e32015-08-19 16:30:16 -0700828 bool needToListen = true;
829 bool enableV4 = true;
830 bool enableV6 = true;
831
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200832 for (const auto& i : configSection) {
833 if (i.first == "port") {
834 port = ConfigFile::parseNumber<uint16_t>(i, "websocket");
835 NFD_LOG_TRACE("WebSocket port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700836 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200837 else if (i.first == "listen") {
838 needToListen = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700839 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200840 else if (i.first == "enable_v4") {
841 enableV4 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700842 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200843 else if (i.first == "enable_v6") {
844 enableV6 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700845 }
846 else {
847 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200848 i.first + "\" in \"websocket\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700849 }
850 }
851
852 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200853 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 WebSocket channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700854 " Remove \"websocket\" section to disable WebSocket channels or"
855 " re-enable at least one channel type."));
856 }
857
858 if (!enableV4 && enableV6) {
859 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel."));
860 }
861
862 if (!isDryRun) {
863 if (m_factories.count("websocket") > 0) {
864 return;
865 }
866
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200867 auto factory = make_shared<WebSocketFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700868 m_factories.insert(std::make_pair("websocket", factory));
869
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200870 shared_ptr<WebSocketChannel> channel;
871
Yanbiao Li73860e32015-08-19 16:30:16 -0700872 if (enableV6 && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200873 websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port);
874 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700875
876 m_factories.insert(std::make_pair("websocket46", factory));
877 }
878 else if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200879 websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port);
880 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700881
882 m_factories.insert(std::make_pair("websocket4", factory));
883 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200884
885 if (channel && needToListen) {
886 channel->listen(bind(&FaceTable::add, &m_faceTable, _1));
887 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700888 }
889#else
890 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, "
891 "cannot process \"websocket\" section"));
892#endif // HAVE_WEBSOCKET
893}
894
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200895} // namespace nfd