blob: 1bc1701304bf66d72b47fa987be4fde2cb5f52bd [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"
Davide Pesavento35120ea2015-11-17 21:13:18 +010045#include "face/ethernet-transport.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070046#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) {
Junxiao Shida93f1f2015-11-11 06:13:16 -0700272 ndn::nfd::FaceStatus status;
273 collectFaceProperties(*face, status);
274 collectFaceCounters(*face, status);
275 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700276 }
277 context.end();
278}
279
280void
281FaceManager::listChannels(const Name& topPrefix, const Interest& interest,
282 ndn::mgmt::StatusDatasetContext& context)
283{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200284 std::set<const ProtocolFactory*> seenFactories;
Yanbiao Li73860e32015-08-19 16:30:16 -0700285
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200286 for (const auto& kv : m_factories) {
287 const ProtocolFactory* factory = kv.second.get();
288 bool inserted;
289 std::tie(std::ignore, inserted) = seenFactories.insert(factory);
Yanbiao Li73860e32015-08-19 16:30:16 -0700290
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200291 if (inserted) {
292 for (const auto& channel : factory->getChannels()) {
293 ndn::nfd::ChannelStatus entry;
294 entry.setLocalUri(channel->getUri().toString());
295 context.append(entry.wireEncode());
296 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700297 }
298 }
299
300 context.end();
301}
302
303void
304FaceManager::queryFaces(const Name& topPrefix, const Interest& interest,
305 ndn::mgmt::StatusDatasetContext& context)
306{
307 ndn::nfd::FaceQueryFilter faceFilter;
308 const Name& query = interest.getName();
309 try {
310 faceFilter.wireDecode(query[-1].blockFromValue());
311 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200312 catch (const tlv::Error& e) {
313 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
314 return context.reject(ControlResponse(400, "Malformed filter"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700315 }
316
317 for (const auto& face : m_faceTable) {
Junxiao Shida93f1f2015-11-11 06:13:16 -0700318 if (!doesMatchFilter(faceFilter, face)) {
319 continue;
Yanbiao Li73860e32015-08-19 16:30:16 -0700320 }
Junxiao Shida93f1f2015-11-11 06:13:16 -0700321 ndn::nfd::FaceStatus status;
322 collectFaceProperties(*face, status);
323 collectFaceCounters(*face, status);
324 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700325 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200326
Yanbiao Li73860e32015-08-19 16:30:16 -0700327 context.end();
328}
329
330bool
331FaceManager::doesMatchFilter(const ndn::nfd::FaceQueryFilter& filter, shared_ptr<Face> face)
332{
333 if (filter.hasFaceId() &&
334 filter.getFaceId() != static_cast<uint64_t>(face->getId())) {
335 return false;
336 }
337
338 if (filter.hasUriScheme() &&
339 filter.getUriScheme() != face->getRemoteUri().getScheme() &&
340 filter.getUriScheme() != face->getLocalUri().getScheme()) {
341 return false;
342 }
343
344 if (filter.hasRemoteUri() &&
345 filter.getRemoteUri() != face->getRemoteUri().toString()) {
346 return false;
347 }
348
349 if (filter.hasLocalUri() &&
350 filter.getLocalUri() != face->getLocalUri().toString()) {
351 return false;
352 }
353
354 if (filter.hasFaceScope() &&
355 (filter.getFaceScope() == ndn::nfd::FACE_SCOPE_LOCAL) != face->isLocal()) {
356 return false;
357 }
358
359 if (filter.hasFacePersistency() &&
360 filter.getFacePersistency() != face->getPersistency()) {
361 return false;
362 }
363
364 if (filter.hasLinkType() &&
365 (filter.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) != face->isMultiAccess()) {
366 return false;
367 }
368
369 return true;
370}
371
Junxiao Shida93f1f2015-11-11 06:13:16 -0700372template<typename FaceTraits>
373inline void
374collectLpFaceProperties(const face::LpFace& face, FaceTraits& traits)
375{
376 traits.setFaceId(face.getId())
377 .setRemoteUri(face.getRemoteUri().toString())
378 .setLocalUri(face.getLocalUri().toString())
379 .setFaceScope(face.getScope())
380 .setFacePersistency(face.getPersistency())
381 .setLinkType(face.getLinkType());
382 // TODO#3172 replace this into FaceManager::collectFaceProperties
383}
384
385template<typename FaceTraits>
386void
387FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits)
388{
389 auto lpFace = dynamic_cast<const face::LpFace*>(&face);
390 if (lpFace != nullptr) {
391 collectLpFaceProperties(*lpFace, traits);
392 return;
393 }
394
395 traits.setFaceId(face.getId())
396 .setRemoteUri(face.getRemoteUri().toString())
397 .setLocalUri(face.getLocalUri().toString())
398 .setFaceScope(face.isLocal() ? ndn::nfd::FACE_SCOPE_LOCAL
399 : ndn::nfd::FACE_SCOPE_NON_LOCAL)
400 .setFacePersistency(face.getPersistency())
401 .setLinkType(face.isMultiAccess() ? ndn::nfd::LINK_TYPE_MULTI_ACCESS
402 : ndn::nfd::LINK_TYPE_POINT_TO_POINT);
403}
404
405void
406FaceManager::collectFaceCounters(const Face& face, ndn::nfd::FaceStatus& status)
407{
408 const face::FaceCounters& counters = face.getCounters();
409 status.setNInInterests(counters.nInInterests)
410 .setNOutInterests(counters.nOutInterests)
411 .setNInDatas(counters.nInData)
412 .setNOutDatas(counters.nOutData)
413 .setNInNacks(counters.nInNacks)
414 .setNOutNacks(counters.nOutNacks)
415 .setNInBytes(counters.nInBytes)
416 .setNOutBytes(counters.nOutBytes);
417}
418
Yanbiao Li73860e32015-08-19 16:30:16 -0700419void
420FaceManager::afterFaceAdded(shared_ptr<Face> face,
421 const ndn::mgmt::PostNotification& post)
422{
423 ndn::nfd::FaceEventNotification notification;
424 notification.setKind(ndn::nfd::FACE_EVENT_CREATED);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700425 collectFaceProperties(*face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700426
427 post(notification.wireEncode());
428}
429
430void
431FaceManager::afterFaceRemoved(shared_ptr<Face> face,
432 const ndn::mgmt::PostNotification& post)
433{
434 ndn::nfd::FaceEventNotification notification;
435 notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700436 collectFaceProperties(*face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700437
438 post(notification.wireEncode());
439}
440
441void
442FaceManager::processConfig(const ConfigSection& configSection,
443 bool isDryRun,
444 const std::string& filename)
445{
446 bool hasSeenUnix = false;
447 bool hasSeenTcp = false;
448 bool hasSeenUdp = false;
449 bool hasSeenEther = false;
450 bool hasSeenWebSocket = false;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200451 auto nicList = listNetworkInterfaces();
Yanbiao Li73860e32015-08-19 16:30:16 -0700452
453 for (const auto& item : configSection) {
454 if (item.first == "unix") {
455 if (hasSeenUnix) {
456 BOOST_THROW_EXCEPTION(Error("Duplicate \"unix\" section"));
457 }
458 hasSeenUnix = true;
459
460 processSectionUnix(item.second, isDryRun);
461 }
462 else if (item.first == "tcp") {
463 if (hasSeenTcp) {
464 BOOST_THROW_EXCEPTION(Error("Duplicate \"tcp\" section"));
465 }
466 hasSeenTcp = true;
467
468 processSectionTcp(item.second, isDryRun);
469 }
470 else if (item.first == "udp") {
471 if (hasSeenUdp) {
472 BOOST_THROW_EXCEPTION(Error("Duplicate \"udp\" section"));
473 }
474 hasSeenUdp = true;
475
476 processSectionUdp(item.second, isDryRun, nicList);
477 }
478 else if (item.first == "ether") {
479 if (hasSeenEther) {
480 BOOST_THROW_EXCEPTION(Error("Duplicate \"ether\" section"));
481 }
482 hasSeenEther = true;
483
484 processSectionEther(item.second, isDryRun, nicList);
485 }
486 else if (item.first == "websocket") {
487 if (hasSeenWebSocket) {
488 BOOST_THROW_EXCEPTION(Error("Duplicate \"websocket\" section"));
489 }
490 hasSeenWebSocket = true;
491
492 processSectionWebSocket(item.second, isDryRun);
493 }
494 else {
495 BOOST_THROW_EXCEPTION(Error("Unrecognized option \"" + item.first + "\""));
496 }
497 }
498}
499
500void
501FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun)
502{
503 // ; the unix section contains settings of Unix stream faces and channels
504 // unix
505 // {
506 // path /var/run/nfd.sock ; Unix stream listener path
507 // }
508
509#if defined(HAVE_UNIX_SOCKETS)
Yanbiao Li73860e32015-08-19 16:30:16 -0700510 std::string path = "/var/run/nfd.sock";
511
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200512 for (const auto& i : configSection) {
513 if (i.first == "path") {
514 path = i.second.get_value<std::string>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700515 }
516 else {
517 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200518 i.first + "\" in \"unix\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700519 }
520 }
521
522 if (!isDryRun) {
523 if (m_factories.count("unix") > 0) {
524 return;
525 }
526
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200527 auto factory = make_shared<UnixStreamFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700528 m_factories.insert(std::make_pair("unix", factory));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200529
530 auto channel = factory->createChannel(path);
531 channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700532 }
533#else
534 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, "
535 "cannot process \"unix\" section"));
536#endif // HAVE_UNIX_SOCKETS
537}
538
539void
540FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun)
541{
542 // ; the tcp section contains settings of TCP faces and channels
543 // tcp
544 // {
545 // listen yes ; set to 'no' to disable TCP listener, default 'yes'
546 // port 6363 ; TCP listener port number
547 // }
548
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200549 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700550 bool needToListen = true;
551 bool enableV4 = true;
552 bool enableV6 = true;
553
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200554 for (const auto& i : configSection) {
555 if (i.first == "port") {
556 port = ConfigFile::parseNumber<uint16_t>(i, "tcp");
557 NFD_LOG_TRACE("TCP port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700558 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200559 else if (i.first == "listen") {
560 needToListen = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700561 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200562 else if (i.first == "enable_v4") {
563 enableV4 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700564 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200565 else if (i.first == "enable_v6") {
566 enableV6 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700567 }
568 else {
569 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200570 i.first + "\" in \"tcp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700571 }
572 }
573
574 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200575 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 TCP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700576 " Remove \"tcp\" section to disable TCP channels or"
577 " re-enable at least one channel type."));
578 }
579
580 if (!isDryRun) {
581 if (m_factories.count("tcp") > 0) {
582 return;
583 }
584
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200585 auto factory = make_shared<TcpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700586 m_factories.insert(std::make_pair("tcp", factory));
587
588 if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200589 tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port);
590 shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700591 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200592 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700593 }
594
595 m_factories.insert(std::make_pair("tcp4", factory));
596 }
597
598 if (enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200599 tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port);
600 shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700601 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200602 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700603 }
604
605 m_factories.insert(std::make_pair("tcp6", factory));
606 }
607 }
608}
609
610void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200611FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700612 const std::vector<NetworkInterfaceInfo>& nicList)
613{
614 // ; the udp section contains settings of UDP faces and channels
615 // udp
616 // {
617 // port 6363 ; UDP unicast port number
618 // idle_timeout 600 ; idle time (seconds) before closing a UDP unicast face
619 // keep_alive_interval 25 ; interval (seconds) between keep-alive refreshes
620
621 // ; NFD creates one UDP multicast face per NIC
622 // mcast yes ; set to 'no' to disable UDP multicast, default 'yes'
623 // mcast_port 56363 ; UDP multicast port number
624 // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only)
625 // }
626
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200627 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700628 bool enableV4 = true;
629 bool enableV6 = true;
630 size_t timeout = 600;
631 size_t keepAliveInterval = 25;
632 bool useMcast = true;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200633 auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170");
634 uint16_t mcastPort = 56363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700635
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200636 for (const auto& i : configSection) {
637 if (i.first == "port") {
638 port = ConfigFile::parseNumber<uint16_t>(i, "udp");
639 NFD_LOG_TRACE("UDP unicast port set to " << port);
640 }
641 else if (i.first == "enable_v4") {
642 enableV4 = ConfigFile::parseYesNo(i, "udp");
643 }
644 else if (i.first == "enable_v6") {
645 enableV6 = ConfigFile::parseYesNo(i, "udp");
646 }
647 else if (i.first == "idle_timeout") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700648 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200649 timeout = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700650 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200651 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700652 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200653 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700654 }
655 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200656 else if (i.first == "keep_alive_interval") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700657 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200658 keepAliveInterval = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700659 /// \todo Make use of keepAliveInterval
Yanbiao Li73860e32015-08-19 16:30:16 -0700660 (void)(keepAliveInterval);
661 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200662 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700663 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200664 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700665 }
666 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200667 else if (i.first == "mcast") {
668 useMcast = ConfigFile::parseYesNo(i, "udp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700669 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200670 else if (i.first == "mcast_port") {
671 mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp");
672 NFD_LOG_TRACE("UDP multicast port set to " << mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700673 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200674 else if (i.first == "mcast_group") {
675 boost::system::error_code ec;
676 mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec);
677 if (ec) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700678 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200679 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700680 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200681 NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700682 }
683 else {
684 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200685 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700686 }
687 }
688
689 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200690 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 UDP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700691 " Remove \"udp\" section to disable UDP channels or"
692 " re-enable at least one channel type."));
693 }
694 else if (useMcast && !enableV4) {
695 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 multicast requested, but IPv4 channels"
696 " have been disabled (conflicting configuration options set)"));
697 }
698
699 if (!isDryRun) {
700 shared_ptr<UdpFactory> factory;
701 bool isReload = false;
702 if (m_factories.count("udp") > 0) {
703 isReload = true;
704 factory = static_pointer_cast<UdpFactory>(m_factories["udp"]);
705 }
706 else {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200707 factory = make_shared<UdpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700708 m_factories.insert(std::make_pair("udp", factory));
709 }
710
711 if (!isReload && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200712 udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port);
713 shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout));
714 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700715
716 m_factories.insert(std::make_pair("udp4", factory));
717 }
718
719 if (!isReload && enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200720 udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port);
721 shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout));
722 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700723
Yanbiao Li73860e32015-08-19 16:30:16 -0700724 m_factories.insert(std::make_pair("udp6", factory));
725 }
726
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200727 std::set<shared_ptr<face::LpFaceWrapper>> multicastFacesToRemove;
728 for (const auto& i : factory->getMulticastFaces()) {
729 multicastFacesToRemove.insert(i.second);
730 }
731
Yanbiao Li73860e32015-08-19 16:30:16 -0700732 if (useMcast && enableV4) {
733 std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces;
734 for (const auto& nic : nicList) {
735 if (nic.isUp() && nic.isMulticastCapable() && !nic.ipv4Addresses.empty()) {
736 ipv4MulticastInterfaces.push_back(nic);
737 }
738 }
739
740 bool isNicNameNecessary = false;
741#if defined(__linux__)
742 if (ipv4MulticastInterfaces.size() > 1) {
743 // On Linux if we have more than one MulticastUdpFace
744 // we need to specify the name of the interface
745 isNicNameNecessary = true;
746 }
747#endif
748
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200749 udp::Endpoint mcastEndpoint(mcastGroup, mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700750 for (const auto& nic : ipv4MulticastInterfaces) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200751 udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort);
752 auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint,
Yukai Tu0a49d342015-09-13 12:54:22 +0800753 isNicNameNecessary ? nic.name : "");
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200754 m_faceTable.add(newFace);
755 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700756 }
757 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700758
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200759 for (const auto& face : multicastFacesToRemove) {
760 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700761 }
762 }
763}
764
765void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200766FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700767 const std::vector<NetworkInterfaceInfo>& nicList)
768{
769 // ; the ether section contains settings of Ethernet faces and channels
770 // ether
771 // {
772 // ; NFD creates one Ethernet multicast face per NIC
773 // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes'
774 // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group
775 // }
776
777#if defined(HAVE_LIBPCAP)
778 bool useMcast = true;
779 ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress());
780
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200781 for (const auto& i : configSection) {
782 if (i.first == "mcast") {
783 useMcast = ConfigFile::parseYesNo(i, "ether");
Yanbiao Li73860e32015-08-19 16:30:16 -0700784 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200785 else if (i.first == "mcast_group") {
786 mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>());
Yanbiao Li73860e32015-08-19 16:30:16 -0700787 if (mcastGroup.isNull()) {
788 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200789 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700790 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200791 NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700792 }
793 else {
794 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200795 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700796 }
797 }
798
799 if (!isDryRun) {
800 shared_ptr<EthernetFactory> factory;
801 if (m_factories.count("ether") > 0) {
802 factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]);
803 }
804 else {
805 factory = make_shared<EthernetFactory>();
806 m_factories.insert(std::make_pair("ether", factory));
807 }
808
Davide Pesavento35120ea2015-11-17 21:13:18 +0100809 std::set<shared_ptr<face::LpFaceWrapper>> multicastFacesToRemove;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200810 for (const auto& i : factory->getMulticastFaces()) {
811 multicastFacesToRemove.insert(i.second);
812 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700813
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200814 if (useMcast) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700815 for (const auto& nic : nicList) {
816 if (nic.isUp() && nic.isMulticastCapable()) {
817 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200818 auto newFace = factory->createMulticastFace(nic, mcastGroup);
819 m_faceTable.add(newFace);
820 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700821 }
822 catch (const EthernetFactory::Error& factoryError) {
823 NFD_LOG_ERROR(factoryError.what() << ", continuing");
824 }
Davide Pesavento35120ea2015-11-17 21:13:18 +0100825 catch (const face::EthernetTransport::Error& faceError) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700826 NFD_LOG_ERROR(faceError.what() << ", continuing");
827 }
828 }
829 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700830 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700831
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200832 for (const auto& face : multicastFacesToRemove) {
833 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700834 }
835 }
836#else
837 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section"));
838#endif // HAVE_LIBPCAP
839}
840
841void
842FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun)
843{
844 // ; the websocket section contains settings of WebSocket faces and channels
845 // websocket
846 // {
847 // listen yes ; set to 'no' to disable WebSocket listener, default 'yes'
848 // port 9696 ; WebSocket listener port number
849 // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes'
850 // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes'
851 // }
852
853#if defined(HAVE_WEBSOCKET)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200854 uint16_t port = 9696;
Yanbiao Li73860e32015-08-19 16:30:16 -0700855 bool needToListen = true;
856 bool enableV4 = true;
857 bool enableV6 = true;
858
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200859 for (const auto& i : configSection) {
860 if (i.first == "port") {
861 port = ConfigFile::parseNumber<uint16_t>(i, "websocket");
862 NFD_LOG_TRACE("WebSocket port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700863 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200864 else if (i.first == "listen") {
865 needToListen = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700866 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200867 else if (i.first == "enable_v4") {
868 enableV4 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700869 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200870 else if (i.first == "enable_v6") {
871 enableV6 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700872 }
873 else {
874 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200875 i.first + "\" in \"websocket\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700876 }
877 }
878
879 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200880 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 WebSocket channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700881 " Remove \"websocket\" section to disable WebSocket channels or"
882 " re-enable at least one channel type."));
883 }
884
885 if (!enableV4 && enableV6) {
886 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel."));
887 }
888
889 if (!isDryRun) {
890 if (m_factories.count("websocket") > 0) {
891 return;
892 }
893
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200894 auto factory = make_shared<WebSocketFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700895 m_factories.insert(std::make_pair("websocket", factory));
896
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200897 shared_ptr<WebSocketChannel> channel;
898
Yanbiao Li73860e32015-08-19 16:30:16 -0700899 if (enableV6 && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200900 websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port);
901 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700902
903 m_factories.insert(std::make_pair("websocket46", factory));
904 }
905 else if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200906 websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port);
907 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700908
909 m_factories.insert(std::make_pair("websocket4", factory));
910 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200911
912 if (channel && needToListen) {
913 channel->listen(bind(&FaceTable::add, &m_faceTable, _1));
914 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700915 }
916#else
917 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, "
918 "cannot process \"websocket\" section"));
919#endif // HAVE_WEBSOCKET
920}
921
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200922} // namespace nfd