blob: 641333466d7284646b2f0d499de7fc6bd4fa7ffe [file] [log] [blame]
Yanbiao Li73860e32015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "face-manager.hpp"
27
28#include "core/network-interface.hpp"
Junxiao Shi40cb61c2015-09-23 18:36:45 -070029#include "face/generic-link-service.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080030#include "face/lp-face-wrapper.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070031#include "face/tcp-factory.hpp"
32#include "face/udp-factory.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080033#include "fw/face-table.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070034
Yanbiao Li73860e32015-08-19 16:30:16 -070035#include <ndn-cxx/management/nfd-channel-status.hpp>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020036#include <ndn-cxx/management/nfd-face-status.hpp>
Yanbiao Li73860e32015-08-19 16:30:16 -070037#include <ndn-cxx/management/nfd-face-event-notification.hpp>
38
39#ifdef HAVE_UNIX_SOCKETS
40#include "face/unix-stream-factory.hpp"
41#endif // HAVE_UNIX_SOCKETS
42
43#ifdef HAVE_LIBPCAP
44#include "face/ethernet-factory.hpp"
45#include "face/ethernet-face.hpp"
46#endif // HAVE_LIBPCAP
47
48#ifdef HAVE_WEBSOCKET
49#include "face/websocket-factory.hpp"
50#endif // HAVE_WEBSOCKET
51
52namespace nfd {
53
54NFD_LOG_INIT("FaceManager");
55
56FaceManager::FaceManager(FaceTable& faceTable,
57 Dispatcher& dispatcher,
58 CommandValidator& validator)
59 : ManagerBase(dispatcher, validator, "faces")
60 , m_faceTable(faceTable)
61{
62 registerCommandHandler<ndn::nfd::FaceCreateCommand>("create",
63 bind(&FaceManager::createFace, this, _2, _3, _4, _5));
64
65 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
78 auto postNotification = registerNotificationStream("events");
79 m_faceAddConn =
80 m_faceTable.onAdd.connect(bind(&FaceManager::afterFaceAdded, this, _1, postNotification));
81 m_faceRemoveConn =
82 m_faceTable.onRemove.connect(bind(&FaceManager::afterFaceRemoved, this, _1, postNotification));
83}
84
85void
86FaceManager::setConfigFile(ConfigFile& configFile)
87{
88 configFile.addSectionHandler("face_system", bind(&FaceManager::processConfig, this, _1, _2, _3));
89}
90
91void
92FaceManager::createFace(const Name& topPrefix, const Interest& interest,
93 const ControlParameters& parameters,
94 const ndn::mgmt::CommandContinuation& done)
95{
96 FaceUri uri;
97 if (!uri.parse(parameters.getUri())) {
98 NFD_LOG_TRACE("failed to parse URI");
99 return done(ControlResponse(400, "Malformed command"));
100 }
101
102 if (!uri.isCanonical()) {
103 NFD_LOG_TRACE("received non-canonical URI");
104 return done(ControlResponse(400, "Non-canonical URI"));
105 }
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()) {
109 return done(ControlResponse(501, "Unsupported protocol"));
110 }
111
112 try {
113 factory->second->createFace(uri,
114 parameters.getFacePersistency(),
115 bind(&FaceManager::afterCreateFaceSuccess,
116 this, parameters, _1, done),
117 bind(&FaceManager::afterCreateFaceFailure,
118 this, _1, done));
119 }
120 catch (const std::runtime_error& error) {
121 std::string errorMessage = "Face creation failed: ";
122 errorMessage += error.what();
123
124 NFD_LOG_ERROR(errorMessage);
125 return done(ControlResponse(500, errorMessage));
126 }
127 catch (const std::logic_error& error) {
128 std::string errorMessage = "Face creation failed: ";
129 errorMessage += error.what();
130
131 NFD_LOG_ERROR(errorMessage);
132 return done(ControlResponse(500, errorMessage));
133 }
134}
135
136void
Yanbiao Li73860e32015-08-19 16:30:16 -0700137FaceManager::afterCreateFaceSuccess(ControlParameters& parameters,
138 const shared_ptr<Face>& newFace,
139 const ndn::mgmt::CommandContinuation& done)
140{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200141 m_faceTable.add(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700142 parameters.setFaceId(newFace->getId());
143 parameters.setUri(newFace->getRemoteUri().toString());
144 parameters.setFacePersistency(newFace->getPersistency());
145
146 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
147}
148
149void
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700150FaceManager::destroyFace(const Name& topPrefix, const Interest& interest,
151 const ControlParameters& parameters,
152 const ndn::mgmt::CommandContinuation& done)
153{
154 shared_ptr<Face> target = m_faceTable.get(parameters.getFaceId());
155 if (target) {
156 target->close();
157 }
158
159 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
160}
161
162void
Yanbiao Li73860e32015-08-19 16:30:16 -0700163FaceManager::afterCreateFaceFailure(const std::string& reason,
164 const ndn::mgmt::CommandContinuation& done)
165{
166 NFD_LOG_DEBUG("Failed to create face: " << reason);
167
168 done(ControlResponse(408, "Failed to create face: " + reason));
169}
170
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700171void
172FaceManager::enableLocalControl(const Name& topPrefix, const Interest& interest,
173 const ControlParameters& parameters,
174 const ndn::mgmt::CommandContinuation& done)
175{
176 auto result = extractLocalControlParameters(interest, parameters, done);
177 if (!result.isValid) {
178 return;
179 }
180
181 if (result.face) {
182 result.face->setLocalControlHeaderFeature(result.feature, true);
183 return done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
184 }
185
186 // TODO#3226 redesign enable-local-control
187 // For now, enable-local-control will enable all local fields in GenericLinkService.
188 BOOST_ASSERT(result.lpFace != nullptr);
189 auto service = dynamic_cast<face::GenericLinkService*>(result.lpFace->getLinkService());
190 if (service == nullptr) {
191 return done(ControlResponse(503, "LinkService type not supported"));
192 }
193
194 face::GenericLinkService::Options options = service->getOptions();
195 options.allowLocalFields = true;
196 service->setOptions(options);
197
198 return done(ControlResponse(200, "OK: enable all local fields on GenericLinkService")
199 .setBody(parameters.wireEncode()));
200}
201
202void
203FaceManager::disableLocalControl(const Name& topPrefix, const Interest& interest,
204 const ControlParameters& parameters,
205 const ndn::mgmt::CommandContinuation& done)
206{
207 auto result = extractLocalControlParameters(interest, parameters, done);
208 if (!result.isValid) {
209 return;
210 }
211
212 if (result.face) {
213 result.face->setLocalControlHeaderFeature(result.feature, false);
214 return done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
215 }
216
217 // TODO#3226 redesign disable-local-control
218 // For now, disable-local-control will disable all local fields in GenericLinkService.
219 BOOST_ASSERT(result.lpFace != nullptr);
220 auto service = dynamic_cast<face::GenericLinkService*>(result.lpFace->getLinkService());
221 if (service == nullptr) {
222 return done(ControlResponse(503, "LinkService type not supported"));
223 }
224
225 face::GenericLinkService::Options options = service->getOptions();
226 options.allowLocalFields = false;
227 service->setOptions(options);
228
229 return done(ControlResponse(200, "OK: disable all local fields on GenericLinkService")
230 .setBody(parameters.wireEncode()));
231}
232
Yanbiao Li73860e32015-08-19 16:30:16 -0700233FaceManager::ExtractLocalControlParametersResult
234FaceManager::extractLocalControlParameters(const Interest& request,
235 const ControlParameters& parameters,
236 const ndn::mgmt::CommandContinuation& done)
237{
238 ExtractLocalControlParametersResult result;
239 result.isValid = false;
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700240 result.lpFace = nullptr;
Yanbiao Li73860e32015-08-19 16:30:16 -0700241
242 auto face = m_faceTable.get(request.getIncomingFaceId());
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200243 if (face == nullptr) {
244 NFD_LOG_DEBUG("FaceId " << request.getIncomingFaceId() << " not found");
Yanbiao Li73860e32015-08-19 16:30:16 -0700245 done(ControlResponse(410, "Face not found"));
246 return result;
247 }
248
249 if (!face->isLocal()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200250 NFD_LOG_DEBUG("Cannot enable local control on non-local FaceId " << face->getId());
Yanbiao Li73860e32015-08-19 16:30:16 -0700251 done(ControlResponse(412, "Face is non-local"));
252 return result;
253 }
254
255 result.isValid = true;
256 result.face = dynamic_pointer_cast<LocalFace>(face);
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700257 if (result.face == nullptr) {
258 auto lpFaceW = dynamic_pointer_cast<face::LpFaceWrapper>(face);
259 BOOST_ASSERT(lpFaceW != nullptr);
260 result.lpFace = lpFaceW->getLpFace();
261 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200262 result.feature = parameters.getLocalControlFeature();
Yanbiao Li73860e32015-08-19 16:30:16 -0700263
264 return result;
265}
266
267void
268FaceManager::listFaces(const Name& topPrefix, const Interest& interest,
269 ndn::mgmt::StatusDatasetContext& context)
270{
271 for (const auto& face : m_faceTable) {
272 context.append(face->getFaceStatus().wireEncode());
273 }
274 context.end();
275}
276
277void
278FaceManager::listChannels(const Name& topPrefix, const Interest& interest,
279 ndn::mgmt::StatusDatasetContext& context)
280{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200281 std::set<const ProtocolFactory*> seenFactories;
Yanbiao Li73860e32015-08-19 16:30:16 -0700282
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200283 for (const auto& kv : m_factories) {
284 const ProtocolFactory* factory = kv.second.get();
285 bool inserted;
286 std::tie(std::ignore, inserted) = seenFactories.insert(factory);
Yanbiao Li73860e32015-08-19 16:30:16 -0700287
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200288 if (inserted) {
289 for (const auto& channel : factory->getChannels()) {
290 ndn::nfd::ChannelStatus entry;
291 entry.setLocalUri(channel->getUri().toString());
292 context.append(entry.wireEncode());
293 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700294 }
295 }
296
297 context.end();
298}
299
300void
301FaceManager::queryFaces(const Name& topPrefix, const Interest& interest,
302 ndn::mgmt::StatusDatasetContext& context)
303{
304 ndn::nfd::FaceQueryFilter faceFilter;
305 const Name& query = interest.getName();
306 try {
307 faceFilter.wireDecode(query[-1].blockFromValue());
308 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200309 catch (const tlv::Error& e) {
310 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
311 return context.reject(ControlResponse(400, "Malformed filter"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700312 }
313
314 for (const auto& face : m_faceTable) {
315 if (doesMatchFilter(faceFilter, face)) {
316 context.append(face->getFaceStatus().wireEncode());
317 }
318 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200319
Yanbiao Li73860e32015-08-19 16:30:16 -0700320 context.end();
321}
322
323bool
324FaceManager::doesMatchFilter(const ndn::nfd::FaceQueryFilter& filter, shared_ptr<Face> face)
325{
326 if (filter.hasFaceId() &&
327 filter.getFaceId() != static_cast<uint64_t>(face->getId())) {
328 return false;
329 }
330
331 if (filter.hasUriScheme() &&
332 filter.getUriScheme() != face->getRemoteUri().getScheme() &&
333 filter.getUriScheme() != face->getLocalUri().getScheme()) {
334 return false;
335 }
336
337 if (filter.hasRemoteUri() &&
338 filter.getRemoteUri() != face->getRemoteUri().toString()) {
339 return false;
340 }
341
342 if (filter.hasLocalUri() &&
343 filter.getLocalUri() != face->getLocalUri().toString()) {
344 return false;
345 }
346
347 if (filter.hasFaceScope() &&
348 (filter.getFaceScope() == ndn::nfd::FACE_SCOPE_LOCAL) != face->isLocal()) {
349 return false;
350 }
351
352 if (filter.hasFacePersistency() &&
353 filter.getFacePersistency() != face->getPersistency()) {
354 return false;
355 }
356
357 if (filter.hasLinkType() &&
358 (filter.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) != face->isMultiAccess()) {
359 return false;
360 }
361
362 return true;
363}
364
365void
366FaceManager::afterFaceAdded(shared_ptr<Face> face,
367 const ndn::mgmt::PostNotification& post)
368{
369 ndn::nfd::FaceEventNotification notification;
370 notification.setKind(ndn::nfd::FACE_EVENT_CREATED);
371 face->copyStatusTo(notification);
372
373 post(notification.wireEncode());
374}
375
376void
377FaceManager::afterFaceRemoved(shared_ptr<Face> face,
378 const ndn::mgmt::PostNotification& post)
379{
380 ndn::nfd::FaceEventNotification notification;
381 notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED);
382 face->copyStatusTo(notification);
383
384 post(notification.wireEncode());
385}
386
387void
388FaceManager::processConfig(const ConfigSection& configSection,
389 bool isDryRun,
390 const std::string& filename)
391{
392 bool hasSeenUnix = false;
393 bool hasSeenTcp = false;
394 bool hasSeenUdp = false;
395 bool hasSeenEther = false;
396 bool hasSeenWebSocket = false;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200397 auto nicList = listNetworkInterfaces();
Yanbiao Li73860e32015-08-19 16:30:16 -0700398
399 for (const auto& item : configSection) {
400 if (item.first == "unix") {
401 if (hasSeenUnix) {
402 BOOST_THROW_EXCEPTION(Error("Duplicate \"unix\" section"));
403 }
404 hasSeenUnix = true;
405
406 processSectionUnix(item.second, isDryRun);
407 }
408 else if (item.first == "tcp") {
409 if (hasSeenTcp) {
410 BOOST_THROW_EXCEPTION(Error("Duplicate \"tcp\" section"));
411 }
412 hasSeenTcp = true;
413
414 processSectionTcp(item.second, isDryRun);
415 }
416 else if (item.first == "udp") {
417 if (hasSeenUdp) {
418 BOOST_THROW_EXCEPTION(Error("Duplicate \"udp\" section"));
419 }
420 hasSeenUdp = true;
421
422 processSectionUdp(item.second, isDryRun, nicList);
423 }
424 else if (item.first == "ether") {
425 if (hasSeenEther) {
426 BOOST_THROW_EXCEPTION(Error("Duplicate \"ether\" section"));
427 }
428 hasSeenEther = true;
429
430 processSectionEther(item.second, isDryRun, nicList);
431 }
432 else if (item.first == "websocket") {
433 if (hasSeenWebSocket) {
434 BOOST_THROW_EXCEPTION(Error("Duplicate \"websocket\" section"));
435 }
436 hasSeenWebSocket = true;
437
438 processSectionWebSocket(item.second, isDryRun);
439 }
440 else {
441 BOOST_THROW_EXCEPTION(Error("Unrecognized option \"" + item.first + "\""));
442 }
443 }
444}
445
446void
447FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun)
448{
449 // ; the unix section contains settings of Unix stream faces and channels
450 // unix
451 // {
452 // path /var/run/nfd.sock ; Unix stream listener path
453 // }
454
455#if defined(HAVE_UNIX_SOCKETS)
Yanbiao Li73860e32015-08-19 16:30:16 -0700456 std::string path = "/var/run/nfd.sock";
457
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200458 for (const auto& i : configSection) {
459 if (i.first == "path") {
460 path = i.second.get_value<std::string>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700461 }
462 else {
463 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200464 i.first + "\" in \"unix\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700465 }
466 }
467
468 if (!isDryRun) {
469 if (m_factories.count("unix") > 0) {
470 return;
471 }
472
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200473 auto factory = make_shared<UnixStreamFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700474 m_factories.insert(std::make_pair("unix", factory));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200475
476 auto channel = factory->createChannel(path);
477 channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700478 }
479#else
480 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, "
481 "cannot process \"unix\" section"));
482#endif // HAVE_UNIX_SOCKETS
483}
484
485void
486FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun)
487{
488 // ; the tcp section contains settings of TCP faces and channels
489 // tcp
490 // {
491 // listen yes ; set to 'no' to disable TCP listener, default 'yes'
492 // port 6363 ; TCP listener port number
493 // }
494
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200495 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700496 bool needToListen = true;
497 bool enableV4 = true;
498 bool enableV6 = true;
499
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200500 for (const auto& i : configSection) {
501 if (i.first == "port") {
502 port = ConfigFile::parseNumber<uint16_t>(i, "tcp");
503 NFD_LOG_TRACE("TCP port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700504 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200505 else if (i.first == "listen") {
506 needToListen = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700507 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200508 else if (i.first == "enable_v4") {
509 enableV4 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700510 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200511 else if (i.first == "enable_v6") {
512 enableV6 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700513 }
514 else {
515 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200516 i.first + "\" in \"tcp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700517 }
518 }
519
520 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200521 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 TCP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700522 " Remove \"tcp\" section to disable TCP channels or"
523 " re-enable at least one channel type."));
524 }
525
526 if (!isDryRun) {
527 if (m_factories.count("tcp") > 0) {
528 return;
529 }
530
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200531 auto factory = make_shared<TcpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700532 m_factories.insert(std::make_pair("tcp", factory));
533
534 if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200535 tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port);
536 shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700537 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200538 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700539 }
540
541 m_factories.insert(std::make_pair("tcp4", factory));
542 }
543
544 if (enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200545 tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port);
546 shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700547 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200548 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700549 }
550
551 m_factories.insert(std::make_pair("tcp6", factory));
552 }
553 }
554}
555
556void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200557FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700558 const std::vector<NetworkInterfaceInfo>& nicList)
559{
560 // ; the udp section contains settings of UDP faces and channels
561 // udp
562 // {
563 // port 6363 ; UDP unicast port number
564 // idle_timeout 600 ; idle time (seconds) before closing a UDP unicast face
565 // keep_alive_interval 25 ; interval (seconds) between keep-alive refreshes
566
567 // ; NFD creates one UDP multicast face per NIC
568 // mcast yes ; set to 'no' to disable UDP multicast, default 'yes'
569 // mcast_port 56363 ; UDP multicast port number
570 // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only)
571 // }
572
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200573 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700574 bool enableV4 = true;
575 bool enableV6 = true;
576 size_t timeout = 600;
577 size_t keepAliveInterval = 25;
578 bool useMcast = true;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200579 auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170");
580 uint16_t mcastPort = 56363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700581
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200582 for (const auto& i : configSection) {
583 if (i.first == "port") {
584 port = ConfigFile::parseNumber<uint16_t>(i, "udp");
585 NFD_LOG_TRACE("UDP unicast port set to " << port);
586 }
587 else if (i.first == "enable_v4") {
588 enableV4 = ConfigFile::parseYesNo(i, "udp");
589 }
590 else if (i.first == "enable_v6") {
591 enableV6 = ConfigFile::parseYesNo(i, "udp");
592 }
593 else if (i.first == "idle_timeout") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700594 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200595 timeout = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700596 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200597 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700598 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200599 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700600 }
601 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200602 else if (i.first == "keep_alive_interval") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700603 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200604 keepAliveInterval = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700605 /// \todo Make use of keepAliveInterval
Yanbiao Li73860e32015-08-19 16:30:16 -0700606 (void)(keepAliveInterval);
607 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200608 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700609 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200610 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700611 }
612 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200613 else if (i.first == "mcast") {
614 useMcast = ConfigFile::parseYesNo(i, "udp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700615 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200616 else if (i.first == "mcast_port") {
617 mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp");
618 NFD_LOG_TRACE("UDP multicast port set to " << mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700619 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200620 else if (i.first == "mcast_group") {
621 boost::system::error_code ec;
622 mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec);
623 if (ec) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700624 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200625 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700626 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200627 NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700628 }
629 else {
630 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200631 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700632 }
633 }
634
635 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200636 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 UDP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700637 " Remove \"udp\" section to disable UDP channels or"
638 " re-enable at least one channel type."));
639 }
640 else if (useMcast && !enableV4) {
641 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 multicast requested, but IPv4 channels"
642 " have been disabled (conflicting configuration options set)"));
643 }
644
645 if (!isDryRun) {
646 shared_ptr<UdpFactory> factory;
647 bool isReload = false;
648 if (m_factories.count("udp") > 0) {
649 isReload = true;
650 factory = static_pointer_cast<UdpFactory>(m_factories["udp"]);
651 }
652 else {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200653 factory = make_shared<UdpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700654 m_factories.insert(std::make_pair("udp", factory));
655 }
656
657 if (!isReload && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200658 udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port);
659 shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout));
660 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700661
662 m_factories.insert(std::make_pair("udp4", factory));
663 }
664
665 if (!isReload && enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200666 udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port);
667 shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout));
668 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700669
Yanbiao Li73860e32015-08-19 16:30:16 -0700670 m_factories.insert(std::make_pair("udp6", factory));
671 }
672
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200673 std::set<shared_ptr<face::LpFaceWrapper>> multicastFacesToRemove;
674 for (const auto& i : factory->getMulticastFaces()) {
675 multicastFacesToRemove.insert(i.second);
676 }
677
Yanbiao Li73860e32015-08-19 16:30:16 -0700678 if (useMcast && enableV4) {
679 std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces;
680 for (const auto& nic : nicList) {
681 if (nic.isUp() && nic.isMulticastCapable() && !nic.ipv4Addresses.empty()) {
682 ipv4MulticastInterfaces.push_back(nic);
683 }
684 }
685
686 bool isNicNameNecessary = false;
687#if defined(__linux__)
688 if (ipv4MulticastInterfaces.size() > 1) {
689 // On Linux if we have more than one MulticastUdpFace
690 // we need to specify the name of the interface
691 isNicNameNecessary = true;
692 }
693#endif
694
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200695 udp::Endpoint mcastEndpoint(mcastGroup, mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700696 for (const auto& nic : ipv4MulticastInterfaces) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200697 udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort);
698 auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint,
Yukai Tu0a49d342015-09-13 12:54:22 +0800699 isNicNameNecessary ? nic.name : "");
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200700 m_faceTable.add(newFace);
701 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700702 }
703 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700704
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200705 for (const auto& face : multicastFacesToRemove) {
706 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700707 }
708 }
709}
710
711void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200712FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700713 const std::vector<NetworkInterfaceInfo>& nicList)
714{
715 // ; the ether section contains settings of Ethernet faces and channels
716 // ether
717 // {
718 // ; NFD creates one Ethernet multicast face per NIC
719 // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes'
720 // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group
721 // }
722
723#if defined(HAVE_LIBPCAP)
724 bool useMcast = true;
725 ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress());
726
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200727 for (const auto& i : configSection) {
728 if (i.first == "mcast") {
729 useMcast = ConfigFile::parseYesNo(i, "ether");
Yanbiao Li73860e32015-08-19 16:30:16 -0700730 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200731 else if (i.first == "mcast_group") {
732 mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>());
Yanbiao Li73860e32015-08-19 16:30:16 -0700733 if (mcastGroup.isNull()) {
734 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200735 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700736 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200737 NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700738 }
739 else {
740 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200741 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700742 }
743 }
744
745 if (!isDryRun) {
746 shared_ptr<EthernetFactory> factory;
747 if (m_factories.count("ether") > 0) {
748 factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]);
749 }
750 else {
751 factory = make_shared<EthernetFactory>();
752 m_factories.insert(std::make_pair("ether", factory));
753 }
754
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200755 std::set<shared_ptr<EthernetFace>> multicastFacesToRemove;
756 for (const auto& i : factory->getMulticastFaces()) {
757 multicastFacesToRemove.insert(i.second);
758 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700759
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200760 if (useMcast) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700761 for (const auto& nic : nicList) {
762 if (nic.isUp() && nic.isMulticastCapable()) {
763 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200764 auto newFace = factory->createMulticastFace(nic, mcastGroup);
765 m_faceTable.add(newFace);
766 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700767 }
768 catch (const EthernetFactory::Error& factoryError) {
769 NFD_LOG_ERROR(factoryError.what() << ", continuing");
770 }
771 catch (const EthernetFace::Error& faceError) {
772 NFD_LOG_ERROR(faceError.what() << ", continuing");
773 }
774 }
775 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700776 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700777
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200778 for (const auto& face : multicastFacesToRemove) {
779 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700780 }
781 }
782#else
783 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section"));
784#endif // HAVE_LIBPCAP
785}
786
787void
788FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun)
789{
790 // ; the websocket section contains settings of WebSocket faces and channels
791 // websocket
792 // {
793 // listen yes ; set to 'no' to disable WebSocket listener, default 'yes'
794 // port 9696 ; WebSocket listener port number
795 // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes'
796 // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes'
797 // }
798
799#if defined(HAVE_WEBSOCKET)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200800 uint16_t port = 9696;
Yanbiao Li73860e32015-08-19 16:30:16 -0700801 bool needToListen = true;
802 bool enableV4 = true;
803 bool enableV6 = true;
804
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200805 for (const auto& i : configSection) {
806 if (i.first == "port") {
807 port = ConfigFile::parseNumber<uint16_t>(i, "websocket");
808 NFD_LOG_TRACE("WebSocket port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700809 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200810 else if (i.first == "listen") {
811 needToListen = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700812 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200813 else if (i.first == "enable_v4") {
814 enableV4 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700815 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200816 else if (i.first == "enable_v6") {
817 enableV6 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700818 }
819 else {
820 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200821 i.first + "\" in \"websocket\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700822 }
823 }
824
825 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200826 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 WebSocket channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700827 " Remove \"websocket\" section to disable WebSocket channels or"
828 " re-enable at least one channel type."));
829 }
830
831 if (!enableV4 && enableV6) {
832 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel."));
833 }
834
835 if (!isDryRun) {
836 if (m_factories.count("websocket") > 0) {
837 return;
838 }
839
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200840 auto factory = make_shared<WebSocketFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700841 m_factories.insert(std::make_pair("websocket", factory));
842
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200843 shared_ptr<WebSocketChannel> channel;
844
Yanbiao Li73860e32015-08-19 16:30:16 -0700845 if (enableV6 && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200846 websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port);
847 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700848
849 m_factories.insert(std::make_pair("websocket46", factory));
850 }
851 else if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200852 websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port);
853 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700854
855 m_factories.insert(std::make_pair("websocket4", factory));
856 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200857
858 if (channel && needToListen) {
859 channel->listen(bind(&FaceTable::add, &m_faceTable, _1));
860 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700861 }
862#else
863 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, "
864 "cannot process \"websocket\" section"));
865#endif // HAVE_WEBSOCKET
866}
867
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200868} // namespace nfd