blob: d77320692a78e32713a148a23231fdec8dd537ad [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
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700181 // TODO#3226 redesign enable-local-control
182 // For now, enable-local-control will enable all local fields in GenericLinkService.
183 BOOST_ASSERT(result.lpFace != nullptr);
184 auto service = dynamic_cast<face::GenericLinkService*>(result.lpFace->getLinkService());
185 if (service == nullptr) {
186 return done(ControlResponse(503, "LinkService type not supported"));
187 }
188
189 face::GenericLinkService::Options options = service->getOptions();
190 options.allowLocalFields = true;
191 service->setOptions(options);
192
193 return done(ControlResponse(200, "OK: enable all local fields on GenericLinkService")
194 .setBody(parameters.wireEncode()));
195}
196
197void
198FaceManager::disableLocalControl(const Name& topPrefix, const Interest& interest,
199 const ControlParameters& parameters,
200 const ndn::mgmt::CommandContinuation& done)
201{
202 auto result = extractLocalControlParameters(interest, parameters, done);
203 if (!result.isValid) {
204 return;
205 }
206
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700207 // TODO#3226 redesign disable-local-control
208 // For now, disable-local-control will disable all local fields in GenericLinkService.
209 BOOST_ASSERT(result.lpFace != nullptr);
210 auto service = dynamic_cast<face::GenericLinkService*>(result.lpFace->getLinkService());
211 if (service == nullptr) {
212 return done(ControlResponse(503, "LinkService type not supported"));
213 }
214
215 face::GenericLinkService::Options options = service->getOptions();
216 options.allowLocalFields = false;
217 service->setOptions(options);
218
219 return done(ControlResponse(200, "OK: disable all local fields on GenericLinkService")
220 .setBody(parameters.wireEncode()));
221}
222
Yanbiao Li73860e32015-08-19 16:30:16 -0700223FaceManager::ExtractLocalControlParametersResult
224FaceManager::extractLocalControlParameters(const Interest& request,
225 const ControlParameters& parameters,
226 const ndn::mgmt::CommandContinuation& done)
227{
228 ExtractLocalControlParametersResult result;
229 result.isValid = false;
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700230 result.lpFace = nullptr;
Yanbiao Li73860e32015-08-19 16:30:16 -0700231
Junxiao Shi0de23a22015-12-03 20:07:02 +0000232 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
233 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
234 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
235 // and is initialized synchronously with IncomingFaceId field enabled.
236 BOOST_ASSERT(incomingFaceIdTag != nullptr);
237
238 auto face = m_faceTable.get(*incomingFaceIdTag);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200239 if (face == nullptr) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000240 NFD_LOG_DEBUG("FaceId " << *incomingFaceIdTag << " not found");
Yanbiao Li73860e32015-08-19 16:30:16 -0700241 done(ControlResponse(410, "Face not found"));
242 return result;
243 }
244
245 if (!face->isLocal()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200246 NFD_LOG_DEBUG("Cannot enable local control on non-local FaceId " << face->getId());
Yanbiao Li73860e32015-08-19 16:30:16 -0700247 done(ControlResponse(412, "Face is non-local"));
248 return result;
249 }
250
251 result.isValid = true;
Junxiao Shi0de23a22015-12-03 20:07:02 +0000252 auto lpFaceW = dynamic_pointer_cast<face::LpFaceWrapper>(face);
253 // LocalFace is gone, so a face that is local must be an LpFace.
254 BOOST_ASSERT(lpFaceW != nullptr);
255 result.lpFace = lpFaceW->getLpFace();
Yanbiao Li73860e32015-08-19 16:30:16 -0700256
257 return result;
258}
259
260void
261FaceManager::listFaces(const Name& topPrefix, const Interest& interest,
262 ndn::mgmt::StatusDatasetContext& context)
263{
264 for (const auto& face : m_faceTable) {
Junxiao Shida93f1f2015-11-11 06:13:16 -0700265 ndn::nfd::FaceStatus status;
266 collectFaceProperties(*face, status);
267 collectFaceCounters(*face, status);
268 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700269 }
270 context.end();
271}
272
273void
274FaceManager::listChannels(const Name& topPrefix, const Interest& interest,
275 ndn::mgmt::StatusDatasetContext& context)
276{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200277 std::set<const ProtocolFactory*> seenFactories;
Yanbiao Li73860e32015-08-19 16:30:16 -0700278
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200279 for (const auto& kv : m_factories) {
280 const ProtocolFactory* factory = kv.second.get();
281 bool inserted;
282 std::tie(std::ignore, inserted) = seenFactories.insert(factory);
Yanbiao Li73860e32015-08-19 16:30:16 -0700283
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200284 if (inserted) {
285 for (const auto& channel : factory->getChannels()) {
286 ndn::nfd::ChannelStatus entry;
287 entry.setLocalUri(channel->getUri().toString());
288 context.append(entry.wireEncode());
289 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700290 }
291 }
292
293 context.end();
294}
295
296void
297FaceManager::queryFaces(const Name& topPrefix, const Interest& interest,
298 ndn::mgmt::StatusDatasetContext& context)
299{
300 ndn::nfd::FaceQueryFilter faceFilter;
301 const Name& query = interest.getName();
302 try {
303 faceFilter.wireDecode(query[-1].blockFromValue());
304 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200305 catch (const tlv::Error& e) {
306 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
307 return context.reject(ControlResponse(400, "Malformed filter"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700308 }
309
310 for (const auto& face : m_faceTable) {
Junxiao Shida93f1f2015-11-11 06:13:16 -0700311 if (!doesMatchFilter(faceFilter, face)) {
312 continue;
Yanbiao Li73860e32015-08-19 16:30:16 -0700313 }
Junxiao Shida93f1f2015-11-11 06:13:16 -0700314 ndn::nfd::FaceStatus status;
315 collectFaceProperties(*face, status);
316 collectFaceCounters(*face, status);
317 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700318 }
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
Junxiao Shida93f1f2015-11-11 06:13:16 -0700365template<typename FaceTraits>
366inline void
367collectLpFaceProperties(const face::LpFace& face, FaceTraits& traits)
368{
369 traits.setFaceId(face.getId())
370 .setRemoteUri(face.getRemoteUri().toString())
371 .setLocalUri(face.getLocalUri().toString())
372 .setFaceScope(face.getScope())
373 .setFacePersistency(face.getPersistency())
374 .setLinkType(face.getLinkType());
375 // TODO#3172 replace this into FaceManager::collectFaceProperties
376}
377
378template<typename FaceTraits>
379void
380FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits)
381{
382 auto lpFace = dynamic_cast<const face::LpFace*>(&face);
383 if (lpFace != nullptr) {
384 collectLpFaceProperties(*lpFace, traits);
385 return;
386 }
387
388 traits.setFaceId(face.getId())
389 .setRemoteUri(face.getRemoteUri().toString())
390 .setLocalUri(face.getLocalUri().toString())
391 .setFaceScope(face.isLocal() ? ndn::nfd::FACE_SCOPE_LOCAL
392 : ndn::nfd::FACE_SCOPE_NON_LOCAL)
393 .setFacePersistency(face.getPersistency())
394 .setLinkType(face.isMultiAccess() ? ndn::nfd::LINK_TYPE_MULTI_ACCESS
395 : ndn::nfd::LINK_TYPE_POINT_TO_POINT);
396}
397
398void
399FaceManager::collectFaceCounters(const Face& face, ndn::nfd::FaceStatus& status)
400{
401 const face::FaceCounters& counters = face.getCounters();
402 status.setNInInterests(counters.nInInterests)
403 .setNOutInterests(counters.nOutInterests)
404 .setNInDatas(counters.nInData)
405 .setNOutDatas(counters.nOutData)
406 .setNInNacks(counters.nInNacks)
407 .setNOutNacks(counters.nOutNacks)
408 .setNInBytes(counters.nInBytes)
409 .setNOutBytes(counters.nOutBytes);
410}
411
Yanbiao Li73860e32015-08-19 16:30:16 -0700412void
413FaceManager::afterFaceAdded(shared_ptr<Face> face,
414 const ndn::mgmt::PostNotification& post)
415{
416 ndn::nfd::FaceEventNotification notification;
417 notification.setKind(ndn::nfd::FACE_EVENT_CREATED);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700418 collectFaceProperties(*face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700419
420 post(notification.wireEncode());
421}
422
423void
424FaceManager::afterFaceRemoved(shared_ptr<Face> face,
425 const ndn::mgmt::PostNotification& post)
426{
427 ndn::nfd::FaceEventNotification notification;
428 notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700429 collectFaceProperties(*face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700430
431 post(notification.wireEncode());
432}
433
434void
435FaceManager::processConfig(const ConfigSection& configSection,
436 bool isDryRun,
437 const std::string& filename)
438{
439 bool hasSeenUnix = false;
440 bool hasSeenTcp = false;
441 bool hasSeenUdp = false;
442 bool hasSeenEther = false;
443 bool hasSeenWebSocket = false;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200444 auto nicList = listNetworkInterfaces();
Yanbiao Li73860e32015-08-19 16:30:16 -0700445
446 for (const auto& item : configSection) {
447 if (item.first == "unix") {
448 if (hasSeenUnix) {
449 BOOST_THROW_EXCEPTION(Error("Duplicate \"unix\" section"));
450 }
451 hasSeenUnix = true;
452
453 processSectionUnix(item.second, isDryRun);
454 }
455 else if (item.first == "tcp") {
456 if (hasSeenTcp) {
457 BOOST_THROW_EXCEPTION(Error("Duplicate \"tcp\" section"));
458 }
459 hasSeenTcp = true;
460
461 processSectionTcp(item.second, isDryRun);
462 }
463 else if (item.first == "udp") {
464 if (hasSeenUdp) {
465 BOOST_THROW_EXCEPTION(Error("Duplicate \"udp\" section"));
466 }
467 hasSeenUdp = true;
468
469 processSectionUdp(item.second, isDryRun, nicList);
470 }
471 else if (item.first == "ether") {
472 if (hasSeenEther) {
473 BOOST_THROW_EXCEPTION(Error("Duplicate \"ether\" section"));
474 }
475 hasSeenEther = true;
476
477 processSectionEther(item.second, isDryRun, nicList);
478 }
479 else if (item.first == "websocket") {
480 if (hasSeenWebSocket) {
481 BOOST_THROW_EXCEPTION(Error("Duplicate \"websocket\" section"));
482 }
483 hasSeenWebSocket = true;
484
485 processSectionWebSocket(item.second, isDryRun);
486 }
487 else {
488 BOOST_THROW_EXCEPTION(Error("Unrecognized option \"" + item.first + "\""));
489 }
490 }
491}
492
493void
494FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun)
495{
496 // ; the unix section contains settings of Unix stream faces and channels
497 // unix
498 // {
499 // path /var/run/nfd.sock ; Unix stream listener path
500 // }
501
502#if defined(HAVE_UNIX_SOCKETS)
Yanbiao Li73860e32015-08-19 16:30:16 -0700503 std::string path = "/var/run/nfd.sock";
504
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200505 for (const auto& i : configSection) {
506 if (i.first == "path") {
507 path = i.second.get_value<std::string>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700508 }
509 else {
510 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200511 i.first + "\" in \"unix\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700512 }
513 }
514
515 if (!isDryRun) {
516 if (m_factories.count("unix") > 0) {
517 return;
518 }
519
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200520 auto factory = make_shared<UnixStreamFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700521 m_factories.insert(std::make_pair("unix", factory));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200522
523 auto channel = factory->createChannel(path);
524 channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700525 }
526#else
527 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, "
528 "cannot process \"unix\" section"));
529#endif // HAVE_UNIX_SOCKETS
530}
531
532void
533FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun)
534{
535 // ; the tcp section contains settings of TCP faces and channels
536 // tcp
537 // {
538 // listen yes ; set to 'no' to disable TCP listener, default 'yes'
539 // port 6363 ; TCP listener port number
540 // }
541
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200542 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700543 bool needToListen = true;
544 bool enableV4 = true;
545 bool enableV6 = true;
546
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200547 for (const auto& i : configSection) {
548 if (i.first == "port") {
549 port = ConfigFile::parseNumber<uint16_t>(i, "tcp");
550 NFD_LOG_TRACE("TCP port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700551 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200552 else if (i.first == "listen") {
553 needToListen = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700554 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200555 else if (i.first == "enable_v4") {
556 enableV4 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700557 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200558 else if (i.first == "enable_v6") {
559 enableV6 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700560 }
561 else {
562 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200563 i.first + "\" in \"tcp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700564 }
565 }
566
567 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200568 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 TCP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700569 " Remove \"tcp\" section to disable TCP channels or"
570 " re-enable at least one channel type."));
571 }
572
573 if (!isDryRun) {
574 if (m_factories.count("tcp") > 0) {
575 return;
576 }
577
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200578 auto factory = make_shared<TcpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700579 m_factories.insert(std::make_pair("tcp", factory));
580
581 if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200582 tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port);
583 shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700584 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200585 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700586 }
587
588 m_factories.insert(std::make_pair("tcp4", factory));
589 }
590
591 if (enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200592 tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port);
593 shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700594 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200595 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700596 }
597
598 m_factories.insert(std::make_pair("tcp6", factory));
599 }
600 }
601}
602
603void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200604FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700605 const std::vector<NetworkInterfaceInfo>& nicList)
606{
607 // ; the udp section contains settings of UDP faces and channels
608 // udp
609 // {
610 // port 6363 ; UDP unicast port number
611 // idle_timeout 600 ; idle time (seconds) before closing a UDP unicast face
612 // keep_alive_interval 25 ; interval (seconds) between keep-alive refreshes
613
614 // ; NFD creates one UDP multicast face per NIC
615 // mcast yes ; set to 'no' to disable UDP multicast, default 'yes'
616 // mcast_port 56363 ; UDP multicast port number
617 // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only)
618 // }
619
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200620 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700621 bool enableV4 = true;
622 bool enableV6 = true;
623 size_t timeout = 600;
624 size_t keepAliveInterval = 25;
625 bool useMcast = true;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200626 auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170");
627 uint16_t mcastPort = 56363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700628
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200629 for (const auto& i : configSection) {
630 if (i.first == "port") {
631 port = ConfigFile::parseNumber<uint16_t>(i, "udp");
632 NFD_LOG_TRACE("UDP unicast port set to " << port);
633 }
634 else if (i.first == "enable_v4") {
635 enableV4 = ConfigFile::parseYesNo(i, "udp");
636 }
637 else if (i.first == "enable_v6") {
638 enableV6 = ConfigFile::parseYesNo(i, "udp");
639 }
640 else if (i.first == "idle_timeout") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700641 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200642 timeout = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700643 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200644 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700645 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200646 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700647 }
648 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200649 else if (i.first == "keep_alive_interval") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700650 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200651 keepAliveInterval = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700652 /// \todo Make use of keepAliveInterval
Yanbiao Li73860e32015-08-19 16:30:16 -0700653 (void)(keepAliveInterval);
654 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200655 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700656 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200657 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700658 }
659 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200660 else if (i.first == "mcast") {
661 useMcast = ConfigFile::parseYesNo(i, "udp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700662 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200663 else if (i.first == "mcast_port") {
664 mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp");
665 NFD_LOG_TRACE("UDP multicast port set to " << mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700666 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200667 else if (i.first == "mcast_group") {
668 boost::system::error_code ec;
669 mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec);
670 if (ec) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700671 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200672 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700673 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200674 NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700675 }
676 else {
677 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200678 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700679 }
680 }
681
682 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200683 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 UDP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700684 " Remove \"udp\" section to disable UDP channels or"
685 " re-enable at least one channel type."));
686 }
687 else if (useMcast && !enableV4) {
688 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 multicast requested, but IPv4 channels"
689 " have been disabled (conflicting configuration options set)"));
690 }
691
692 if (!isDryRun) {
693 shared_ptr<UdpFactory> factory;
694 bool isReload = false;
695 if (m_factories.count("udp") > 0) {
696 isReload = true;
697 factory = static_pointer_cast<UdpFactory>(m_factories["udp"]);
698 }
699 else {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200700 factory = make_shared<UdpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700701 m_factories.insert(std::make_pair("udp", factory));
702 }
703
704 if (!isReload && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200705 udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port);
706 shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout));
707 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700708
709 m_factories.insert(std::make_pair("udp4", factory));
710 }
711
712 if (!isReload && enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200713 udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port);
714 shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout));
715 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700716
Yanbiao Li73860e32015-08-19 16:30:16 -0700717 m_factories.insert(std::make_pair("udp6", factory));
718 }
719
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200720 std::set<shared_ptr<face::LpFaceWrapper>> multicastFacesToRemove;
721 for (const auto& i : factory->getMulticastFaces()) {
722 multicastFacesToRemove.insert(i.second);
723 }
724
Yanbiao Li73860e32015-08-19 16:30:16 -0700725 if (useMcast && enableV4) {
726 std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces;
727 for (const auto& nic : nicList) {
728 if (nic.isUp() && nic.isMulticastCapable() && !nic.ipv4Addresses.empty()) {
729 ipv4MulticastInterfaces.push_back(nic);
730 }
731 }
732
733 bool isNicNameNecessary = false;
734#if defined(__linux__)
735 if (ipv4MulticastInterfaces.size() > 1) {
736 // On Linux if we have more than one MulticastUdpFace
737 // we need to specify the name of the interface
738 isNicNameNecessary = true;
739 }
740#endif
741
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200742 udp::Endpoint mcastEndpoint(mcastGroup, mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700743 for (const auto& nic : ipv4MulticastInterfaces) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200744 udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort);
745 auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint,
Yukai Tu0a49d342015-09-13 12:54:22 +0800746 isNicNameNecessary ? nic.name : "");
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200747 m_faceTable.add(newFace);
748 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700749 }
750 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700751
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200752 for (const auto& face : multicastFacesToRemove) {
753 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700754 }
755 }
756}
757
758void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200759FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700760 const std::vector<NetworkInterfaceInfo>& nicList)
761{
762 // ; the ether section contains settings of Ethernet faces and channels
763 // ether
764 // {
765 // ; NFD creates one Ethernet multicast face per NIC
766 // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes'
767 // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group
768 // }
769
770#if defined(HAVE_LIBPCAP)
771 bool useMcast = true;
772 ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress());
773
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200774 for (const auto& i : configSection) {
775 if (i.first == "mcast") {
776 useMcast = ConfigFile::parseYesNo(i, "ether");
Yanbiao Li73860e32015-08-19 16:30:16 -0700777 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200778 else if (i.first == "mcast_group") {
779 mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>());
Yanbiao Li73860e32015-08-19 16:30:16 -0700780 if (mcastGroup.isNull()) {
781 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200782 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700783 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200784 NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700785 }
786 else {
787 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200788 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700789 }
790 }
791
792 if (!isDryRun) {
793 shared_ptr<EthernetFactory> factory;
794 if (m_factories.count("ether") > 0) {
795 factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]);
796 }
797 else {
798 factory = make_shared<EthernetFactory>();
799 m_factories.insert(std::make_pair("ether", factory));
800 }
801
Davide Pesavento35120ea2015-11-17 21:13:18 +0100802 std::set<shared_ptr<face::LpFaceWrapper>> multicastFacesToRemove;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200803 for (const auto& i : factory->getMulticastFaces()) {
804 multicastFacesToRemove.insert(i.second);
805 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700806
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200807 if (useMcast) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700808 for (const auto& nic : nicList) {
809 if (nic.isUp() && nic.isMulticastCapable()) {
810 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200811 auto newFace = factory->createMulticastFace(nic, mcastGroup);
812 m_faceTable.add(newFace);
813 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700814 }
815 catch (const EthernetFactory::Error& factoryError) {
816 NFD_LOG_ERROR(factoryError.what() << ", continuing");
817 }
Davide Pesavento35120ea2015-11-17 21:13:18 +0100818 catch (const face::EthernetTransport::Error& faceError) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700819 NFD_LOG_ERROR(faceError.what() << ", continuing");
820 }
821 }
822 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700823 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700824
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200825 for (const auto& face : multicastFacesToRemove) {
826 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700827 }
828 }
829#else
830 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section"));
831#endif // HAVE_LIBPCAP
832}
833
834void
835FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun)
836{
837 // ; the websocket section contains settings of WebSocket faces and channels
838 // websocket
839 // {
840 // listen yes ; set to 'no' to disable WebSocket listener, default 'yes'
841 // port 9696 ; WebSocket listener port number
842 // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes'
843 // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes'
844 // }
845
846#if defined(HAVE_WEBSOCKET)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200847 uint16_t port = 9696;
Yanbiao Li73860e32015-08-19 16:30:16 -0700848 bool needToListen = true;
849 bool enableV4 = true;
850 bool enableV6 = true;
851
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200852 for (const auto& i : configSection) {
853 if (i.first == "port") {
854 port = ConfigFile::parseNumber<uint16_t>(i, "websocket");
855 NFD_LOG_TRACE("WebSocket port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700856 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200857 else if (i.first == "listen") {
858 needToListen = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700859 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200860 else if (i.first == "enable_v4") {
861 enableV4 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700862 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200863 else if (i.first == "enable_v6") {
864 enableV6 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700865 }
866 else {
867 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200868 i.first + "\" in \"websocket\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700869 }
870 }
871
872 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200873 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 WebSocket channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700874 " Remove \"websocket\" section to disable WebSocket channels or"
875 " re-enable at least one channel type."));
876 }
877
878 if (!enableV4 && enableV6) {
879 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel."));
880 }
881
882 if (!isDryRun) {
883 if (m_factories.count("websocket") > 0) {
884 return;
885 }
886
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200887 auto factory = make_shared<WebSocketFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700888 m_factories.insert(std::make_pair("websocket", factory));
889
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200890 shared_ptr<WebSocketChannel> channel;
891
Yanbiao Li73860e32015-08-19 16:30:16 -0700892 if (enableV6 && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200893 websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port);
894 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700895
896 m_factories.insert(std::make_pair("websocket46", factory));
897 }
898 else if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200899 websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port);
900 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700901
902 m_factories.insert(std::make_pair("websocket4", factory));
903 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200904
905 if (channel && needToListen) {
906 channel->listen(bind(&FaceTable::add, &m_faceTable, _1));
907 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700908 }
909#else
910 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, "
911 "cannot process \"websocket\" section"));
912#endif // HAVE_WEBSOCKET
913}
914
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200915} // namespace nfd