blob: 0cc40d514e34f055a01b6999070dcb609deb396d [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"
Yanbiao Li73860e32015-08-19 16:30:16 -070030#include "face/tcp-factory.hpp"
31#include "face/udp-factory.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080032#include "fw/face-table.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070033
Yanbiao Li73860e32015-08-19 16:30:16 -070034#include <ndn-cxx/management/nfd-channel-status.hpp>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020035#include <ndn-cxx/management/nfd-face-status.hpp>
Yanbiao Li73860e32015-08-19 16:30:16 -070036#include <ndn-cxx/management/nfd-face-event-notification.hpp>
37
38#ifdef HAVE_UNIX_SOCKETS
39#include "face/unix-stream-factory.hpp"
40#endif // HAVE_UNIX_SOCKETS
41
42#ifdef HAVE_LIBPCAP
43#include "face/ethernet-factory.hpp"
Davide Pesavento35120ea2015-11-17 21:13:18 +010044#include "face/ethernet-transport.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070045#endif // HAVE_LIBPCAP
46
47#ifdef HAVE_WEBSOCKET
48#include "face/websocket-factory.hpp"
49#endif // HAVE_WEBSOCKET
50
51namespace nfd {
52
53NFD_LOG_INIT("FaceManager");
54
55FaceManager::FaceManager(FaceTable& faceTable,
56 Dispatcher& dispatcher,
57 CommandValidator& validator)
58 : ManagerBase(dispatcher, validator, "faces")
59 , m_faceTable(faceTable)
60{
61 registerCommandHandler<ndn::nfd::FaceCreateCommand>("create",
62 bind(&FaceManager::createFace, this, _2, _3, _4, _5));
63
64 registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy",
65 bind(&FaceManager::destroyFace, this, _2, _3, _4, _5));
66
67 registerCommandHandler<ndn::nfd::FaceEnableLocalControlCommand>("enable-local-control",
68 bind(&FaceManager::enableLocalControl, this, _2, _3, _4, _5));
69
70 registerCommandHandler<ndn::nfd::FaceDisableLocalControlCommand>("disable-local-control",
71 bind(&FaceManager::disableLocalControl, this, _2, _3, _4, _5));
72
73 registerStatusDatasetHandler("list", bind(&FaceManager::listFaces, this, _1, _2, _3));
74 registerStatusDatasetHandler("channels", bind(&FaceManager::listChannels, this, _1, _2, _3));
75 registerStatusDatasetHandler("query", bind(&FaceManager::queryFaces, this, _1, _2, _3));
76
77 auto postNotification = registerNotificationStream("events");
78 m_faceAddConn =
Junxiao Shicde37ad2015-12-24 01:02:05 -070079 m_faceTable.afterAdd.connect(bind(&FaceManager::afterFaceAdded, this, _1, postNotification));
Yanbiao Li73860e32015-08-19 16:30:16 -070080 m_faceRemoveConn =
Junxiao Shicde37ad2015-12-24 01:02:05 -070081 m_faceTable.beforeRemove.connect(bind(&FaceManager::afterFaceRemoved, this, _1, postNotification));
Yanbiao Li73860e32015-08-19 16:30:16 -070082}
83
84void
85FaceManager::setConfigFile(ConfigFile& configFile)
86{
87 configFile.addSectionHandler("face_system", bind(&FaceManager::processConfig, this, _1, _2, _3));
88}
89
90void
91FaceManager::createFace(const Name& topPrefix, const Interest& interest,
92 const ControlParameters& parameters,
93 const ndn::mgmt::CommandContinuation& done)
94{
95 FaceUri uri;
96 if (!uri.parse(parameters.getUri())) {
97 NFD_LOG_TRACE("failed to parse URI");
98 return done(ControlResponse(400, "Malformed command"));
99 }
100
101 if (!uri.isCanonical()) {
102 NFD_LOG_TRACE("received non-canonical URI");
103 return done(ControlResponse(400, "Non-canonical URI"));
104 }
105
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200106 auto factory = m_factories.find(uri.getScheme());
Yanbiao Li73860e32015-08-19 16:30:16 -0700107 if (factory == m_factories.end()) {
108 return done(ControlResponse(501, "Unsupported protocol"));
109 }
110
111 try {
112 factory->second->createFace(uri,
113 parameters.getFacePersistency(),
114 bind(&FaceManager::afterCreateFaceSuccess,
115 this, parameters, _1, done),
116 bind(&FaceManager::afterCreateFaceFailure,
117 this, _1, done));
118 }
119 catch (const std::runtime_error& error) {
120 std::string errorMessage = "Face creation failed: ";
121 errorMessage += error.what();
122
123 NFD_LOG_ERROR(errorMessage);
124 return done(ControlResponse(500, errorMessage));
125 }
126 catch (const std::logic_error& error) {
127 std::string errorMessage = "Face creation failed: ";
128 errorMessage += error.what();
129
130 NFD_LOG_ERROR(errorMessage);
131 return done(ControlResponse(500, errorMessage));
132 }
133}
134
135void
Yanbiao Li73860e32015-08-19 16:30:16 -0700136FaceManager::afterCreateFaceSuccess(ControlParameters& parameters,
137 const shared_ptr<Face>& newFace,
138 const ndn::mgmt::CommandContinuation& done)
139{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200140 m_faceTable.add(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700141 parameters.setFaceId(newFace->getId());
142 parameters.setUri(newFace->getRemoteUri().toString());
143 parameters.setFacePersistency(newFace->getPersistency());
144
145 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
146}
147
148void
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700149FaceManager::destroyFace(const Name& topPrefix, const Interest& interest,
150 const ControlParameters& parameters,
151 const ndn::mgmt::CommandContinuation& done)
152{
153 shared_ptr<Face> target = m_faceTable.get(parameters.getFaceId());
154 if (target) {
155 target->close();
156 }
157
158 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
159}
160
161void
Yanbiao Li73860e32015-08-19 16:30:16 -0700162FaceManager::afterCreateFaceFailure(const std::string& reason,
163 const ndn::mgmt::CommandContinuation& done)
164{
165 NFD_LOG_DEBUG("Failed to create face: " << reason);
166
167 done(ControlResponse(408, "Failed to create face: " + reason));
168}
169
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700170void
171FaceManager::enableLocalControl(const Name& topPrefix, const Interest& interest,
172 const ControlParameters& parameters,
173 const ndn::mgmt::CommandContinuation& done)
174{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700175 Face* face = findFaceForLocalControl(interest, parameters, done);
176 if (!face) {
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700177 return;
178 }
179
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700180 // TODO#3226 redesign enable-local-control
181 // For now, enable-local-control will enable all local fields in GenericLinkService.
Junxiao Shicde37ad2015-12-24 01:02:05 -0700182 auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService());
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700183 if (service == nullptr) {
184 return done(ControlResponse(503, "LinkService type not supported"));
185 }
186
187 face::GenericLinkService::Options options = service->getOptions();
188 options.allowLocalFields = true;
189 service->setOptions(options);
190
191 return done(ControlResponse(200, "OK: enable all local fields on GenericLinkService")
192 .setBody(parameters.wireEncode()));
193}
194
195void
196FaceManager::disableLocalControl(const Name& topPrefix, const Interest& interest,
197 const ControlParameters& parameters,
198 const ndn::mgmt::CommandContinuation& done)
199{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700200 Face* face = findFaceForLocalControl(interest, parameters, done);
201 if (!face) {
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700202 return;
203 }
204
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700205 // TODO#3226 redesign disable-local-control
206 // For now, disable-local-control will disable all local fields in GenericLinkService.
Junxiao Shicde37ad2015-12-24 01:02:05 -0700207 auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService());
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700208 if (service == nullptr) {
209 return done(ControlResponse(503, "LinkService type not supported"));
210 }
211
212 face::GenericLinkService::Options options = service->getOptions();
213 options.allowLocalFields = false;
214 service->setOptions(options);
215
216 return done(ControlResponse(200, "OK: disable all local fields on GenericLinkService")
217 .setBody(parameters.wireEncode()));
218}
219
Junxiao Shicde37ad2015-12-24 01:02:05 -0700220Face*
221FaceManager::findFaceForLocalControl(const Interest& request,
222 const ControlParameters& parameters,
223 const ndn::mgmt::CommandContinuation& done)
Yanbiao Li73860e32015-08-19 16:30:16 -0700224{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000225 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
226 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
227 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
228 // and is initialized synchronously with IncomingFaceId field enabled.
229 BOOST_ASSERT(incomingFaceIdTag != nullptr);
230
231 auto face = m_faceTable.get(*incomingFaceIdTag);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200232 if (face == nullptr) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000233 NFD_LOG_DEBUG("FaceId " << *incomingFaceIdTag << " not found");
Yanbiao Li73860e32015-08-19 16:30:16 -0700234 done(ControlResponse(410, "Face not found"));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700235 return nullptr;
Yanbiao Li73860e32015-08-19 16:30:16 -0700236 }
237
Junxiao Shicde37ad2015-12-24 01:02:05 -0700238 if (face->getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200239 NFD_LOG_DEBUG("Cannot enable local control on non-local FaceId " << face->getId());
Yanbiao Li73860e32015-08-19 16:30:16 -0700240 done(ControlResponse(412, "Face is non-local"));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700241 return nullptr;
Yanbiao Li73860e32015-08-19 16:30:16 -0700242 }
243
Junxiao Shicde37ad2015-12-24 01:02:05 -0700244 return face.get();
Yanbiao Li73860e32015-08-19 16:30:16 -0700245}
246
247void
248FaceManager::listFaces(const Name& topPrefix, const Interest& interest,
249 ndn::mgmt::StatusDatasetContext& context)
250{
251 for (const auto& face : m_faceTable) {
Junxiao Shida93f1f2015-11-11 06:13:16 -0700252 ndn::nfd::FaceStatus status;
253 collectFaceProperties(*face, status);
254 collectFaceCounters(*face, status);
255 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700256 }
257 context.end();
258}
259
260void
261FaceManager::listChannels(const Name& topPrefix, const Interest& interest,
262 ndn::mgmt::StatusDatasetContext& context)
263{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200264 std::set<const ProtocolFactory*> seenFactories;
Yanbiao Li73860e32015-08-19 16:30:16 -0700265
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200266 for (const auto& kv : m_factories) {
267 const ProtocolFactory* factory = kv.second.get();
268 bool inserted;
269 std::tie(std::ignore, inserted) = seenFactories.insert(factory);
Yanbiao Li73860e32015-08-19 16:30:16 -0700270
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200271 if (inserted) {
272 for (const auto& channel : factory->getChannels()) {
273 ndn::nfd::ChannelStatus entry;
274 entry.setLocalUri(channel->getUri().toString());
275 context.append(entry.wireEncode());
276 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700277 }
278 }
279
280 context.end();
281}
282
283void
284FaceManager::queryFaces(const Name& topPrefix, const Interest& interest,
285 ndn::mgmt::StatusDatasetContext& context)
286{
287 ndn::nfd::FaceQueryFilter faceFilter;
288 const Name& query = interest.getName();
289 try {
290 faceFilter.wireDecode(query[-1].blockFromValue());
291 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200292 catch (const tlv::Error& e) {
293 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
294 return context.reject(ControlResponse(400, "Malformed filter"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700295 }
296
297 for (const auto& face : m_faceTable) {
Junxiao Shida93f1f2015-11-11 06:13:16 -0700298 if (!doesMatchFilter(faceFilter, face)) {
299 continue;
Yanbiao Li73860e32015-08-19 16:30:16 -0700300 }
Junxiao Shida93f1f2015-11-11 06:13:16 -0700301 ndn::nfd::FaceStatus status;
302 collectFaceProperties(*face, status);
303 collectFaceCounters(*face, status);
304 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700305 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200306
Yanbiao Li73860e32015-08-19 16:30:16 -0700307 context.end();
308}
309
310bool
311FaceManager::doesMatchFilter(const ndn::nfd::FaceQueryFilter& filter, shared_ptr<Face> face)
312{
313 if (filter.hasFaceId() &&
314 filter.getFaceId() != static_cast<uint64_t>(face->getId())) {
315 return false;
316 }
317
318 if (filter.hasUriScheme() &&
319 filter.getUriScheme() != face->getRemoteUri().getScheme() &&
320 filter.getUriScheme() != face->getLocalUri().getScheme()) {
321 return false;
322 }
323
324 if (filter.hasRemoteUri() &&
325 filter.getRemoteUri() != face->getRemoteUri().toString()) {
326 return false;
327 }
328
329 if (filter.hasLocalUri() &&
330 filter.getLocalUri() != face->getLocalUri().toString()) {
331 return false;
332 }
333
334 if (filter.hasFaceScope() &&
Junxiao Shicde37ad2015-12-24 01:02:05 -0700335 filter.getFaceScope() != face->getScope()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700336 return false;
337 }
338
339 if (filter.hasFacePersistency() &&
340 filter.getFacePersistency() != face->getPersistency()) {
341 return false;
342 }
343
344 if (filter.hasLinkType() &&
Junxiao Shicde37ad2015-12-24 01:02:05 -0700345 filter.getLinkType() != face->getLinkType()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700346 return false;
347 }
348
349 return true;
350}
351
Junxiao Shida93f1f2015-11-11 06:13:16 -0700352template<typename FaceTraits>
Junxiao Shicde37ad2015-12-24 01:02:05 -0700353void
354FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits)
Junxiao Shida93f1f2015-11-11 06:13:16 -0700355{
356 traits.setFaceId(face.getId())
357 .setRemoteUri(face.getRemoteUri().toString())
358 .setLocalUri(face.getLocalUri().toString())
359 .setFaceScope(face.getScope())
360 .setFacePersistency(face.getPersistency())
361 .setLinkType(face.getLinkType());
Junxiao Shida93f1f2015-11-11 06:13:16 -0700362}
363
364void
365FaceManager::collectFaceCounters(const Face& face, ndn::nfd::FaceStatus& status)
366{
367 const face::FaceCounters& counters = face.getCounters();
368 status.setNInInterests(counters.nInInterests)
369 .setNOutInterests(counters.nOutInterests)
370 .setNInDatas(counters.nInData)
371 .setNOutDatas(counters.nOutData)
372 .setNInNacks(counters.nInNacks)
373 .setNOutNacks(counters.nOutNacks)
374 .setNInBytes(counters.nInBytes)
375 .setNOutBytes(counters.nOutBytes);
376}
377
Yanbiao Li73860e32015-08-19 16:30:16 -0700378void
379FaceManager::afterFaceAdded(shared_ptr<Face> face,
380 const ndn::mgmt::PostNotification& post)
381{
382 ndn::nfd::FaceEventNotification notification;
383 notification.setKind(ndn::nfd::FACE_EVENT_CREATED);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700384 collectFaceProperties(*face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700385
386 post(notification.wireEncode());
387}
388
389void
390FaceManager::afterFaceRemoved(shared_ptr<Face> face,
391 const ndn::mgmt::PostNotification& post)
392{
393 ndn::nfd::FaceEventNotification notification;
394 notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700395 collectFaceProperties(*face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700396
397 post(notification.wireEncode());
398}
399
400void
401FaceManager::processConfig(const ConfigSection& configSection,
402 bool isDryRun,
403 const std::string& filename)
404{
405 bool hasSeenUnix = false;
406 bool hasSeenTcp = false;
407 bool hasSeenUdp = false;
408 bool hasSeenEther = false;
409 bool hasSeenWebSocket = false;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200410 auto nicList = listNetworkInterfaces();
Yanbiao Li73860e32015-08-19 16:30:16 -0700411
412 for (const auto& item : configSection) {
413 if (item.first == "unix") {
414 if (hasSeenUnix) {
415 BOOST_THROW_EXCEPTION(Error("Duplicate \"unix\" section"));
416 }
417 hasSeenUnix = true;
418
419 processSectionUnix(item.second, isDryRun);
420 }
421 else if (item.first == "tcp") {
422 if (hasSeenTcp) {
423 BOOST_THROW_EXCEPTION(Error("Duplicate \"tcp\" section"));
424 }
425 hasSeenTcp = true;
426
427 processSectionTcp(item.second, isDryRun);
428 }
429 else if (item.first == "udp") {
430 if (hasSeenUdp) {
431 BOOST_THROW_EXCEPTION(Error("Duplicate \"udp\" section"));
432 }
433 hasSeenUdp = true;
434
435 processSectionUdp(item.second, isDryRun, nicList);
436 }
437 else if (item.first == "ether") {
438 if (hasSeenEther) {
439 BOOST_THROW_EXCEPTION(Error("Duplicate \"ether\" section"));
440 }
441 hasSeenEther = true;
442
443 processSectionEther(item.second, isDryRun, nicList);
444 }
445 else if (item.first == "websocket") {
446 if (hasSeenWebSocket) {
447 BOOST_THROW_EXCEPTION(Error("Duplicate \"websocket\" section"));
448 }
449 hasSeenWebSocket = true;
450
451 processSectionWebSocket(item.second, isDryRun);
452 }
453 else {
454 BOOST_THROW_EXCEPTION(Error("Unrecognized option \"" + item.first + "\""));
455 }
456 }
457}
458
459void
460FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun)
461{
462 // ; the unix section contains settings of Unix stream faces and channels
463 // unix
464 // {
465 // path /var/run/nfd.sock ; Unix stream listener path
466 // }
467
468#if defined(HAVE_UNIX_SOCKETS)
Yanbiao Li73860e32015-08-19 16:30:16 -0700469 std::string path = "/var/run/nfd.sock";
470
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200471 for (const auto& i : configSection) {
472 if (i.first == "path") {
473 path = i.second.get_value<std::string>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700474 }
475 else {
476 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200477 i.first + "\" in \"unix\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700478 }
479 }
480
481 if (!isDryRun) {
482 if (m_factories.count("unix") > 0) {
483 return;
484 }
485
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200486 auto factory = make_shared<UnixStreamFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700487 m_factories.insert(std::make_pair("unix", factory));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200488
489 auto channel = factory->createChannel(path);
490 channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700491 }
492#else
493 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, "
494 "cannot process \"unix\" section"));
495#endif // HAVE_UNIX_SOCKETS
496}
497
498void
499FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun)
500{
501 // ; the tcp section contains settings of TCP faces and channels
502 // tcp
503 // {
504 // listen yes ; set to 'no' to disable TCP listener, default 'yes'
505 // port 6363 ; TCP listener port number
506 // }
507
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200508 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700509 bool needToListen = true;
510 bool enableV4 = true;
511 bool enableV6 = true;
512
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200513 for (const auto& i : configSection) {
514 if (i.first == "port") {
515 port = ConfigFile::parseNumber<uint16_t>(i, "tcp");
516 NFD_LOG_TRACE("TCP port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700517 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200518 else if (i.first == "listen") {
519 needToListen = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700520 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200521 else if (i.first == "enable_v4") {
522 enableV4 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700523 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200524 else if (i.first == "enable_v6") {
525 enableV6 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700526 }
527 else {
528 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200529 i.first + "\" in \"tcp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700530 }
531 }
532
533 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200534 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 TCP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700535 " Remove \"tcp\" section to disable TCP channels or"
536 " re-enable at least one channel type."));
537 }
538
539 if (!isDryRun) {
540 if (m_factories.count("tcp") > 0) {
541 return;
542 }
543
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200544 auto factory = make_shared<TcpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700545 m_factories.insert(std::make_pair("tcp", factory));
546
547 if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200548 tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port);
549 shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700550 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200551 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700552 }
553
554 m_factories.insert(std::make_pair("tcp4", factory));
555 }
556
557 if (enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200558 tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port);
559 shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700560 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200561 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700562 }
563
564 m_factories.insert(std::make_pair("tcp6", factory));
565 }
566 }
567}
568
569void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200570FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700571 const std::vector<NetworkInterfaceInfo>& nicList)
572{
573 // ; the udp section contains settings of UDP faces and channels
574 // udp
575 // {
576 // port 6363 ; UDP unicast port number
577 // idle_timeout 600 ; idle time (seconds) before closing a UDP unicast face
578 // keep_alive_interval 25 ; interval (seconds) between keep-alive refreshes
579
580 // ; NFD creates one UDP multicast face per NIC
581 // mcast yes ; set to 'no' to disable UDP multicast, default 'yes'
582 // mcast_port 56363 ; UDP multicast port number
583 // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only)
584 // }
585
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200586 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700587 bool enableV4 = true;
588 bool enableV6 = true;
589 size_t timeout = 600;
590 size_t keepAliveInterval = 25;
591 bool useMcast = true;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200592 auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170");
593 uint16_t mcastPort = 56363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700594
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200595 for (const auto& i : configSection) {
596 if (i.first == "port") {
597 port = ConfigFile::parseNumber<uint16_t>(i, "udp");
598 NFD_LOG_TRACE("UDP unicast port set to " << port);
599 }
600 else if (i.first == "enable_v4") {
601 enableV4 = ConfigFile::parseYesNo(i, "udp");
602 }
603 else if (i.first == "enable_v6") {
604 enableV6 = ConfigFile::parseYesNo(i, "udp");
605 }
606 else if (i.first == "idle_timeout") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700607 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200608 timeout = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700609 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200610 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700611 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200612 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700613 }
614 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200615 else if (i.first == "keep_alive_interval") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700616 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200617 keepAliveInterval = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700618 /// \todo Make use of keepAliveInterval
Yanbiao Li73860e32015-08-19 16:30:16 -0700619 (void)(keepAliveInterval);
620 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200621 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700622 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200623 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700624 }
625 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200626 else if (i.first == "mcast") {
627 useMcast = ConfigFile::parseYesNo(i, "udp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700628 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200629 else if (i.first == "mcast_port") {
630 mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp");
631 NFD_LOG_TRACE("UDP multicast port set to " << mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700632 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200633 else if (i.first == "mcast_group") {
634 boost::system::error_code ec;
635 mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec);
636 if (ec) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700637 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200638 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700639 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200640 NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700641 }
642 else {
643 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200644 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700645 }
646 }
647
648 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200649 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 UDP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700650 " Remove \"udp\" section to disable UDP channels or"
651 " re-enable at least one channel type."));
652 }
653 else if (useMcast && !enableV4) {
654 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 multicast requested, but IPv4 channels"
655 " have been disabled (conflicting configuration options set)"));
656 }
657
658 if (!isDryRun) {
659 shared_ptr<UdpFactory> factory;
660 bool isReload = false;
661 if (m_factories.count("udp") > 0) {
662 isReload = true;
663 factory = static_pointer_cast<UdpFactory>(m_factories["udp"]);
664 }
665 else {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200666 factory = make_shared<UdpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700667 m_factories.insert(std::make_pair("udp", factory));
668 }
669
670 if (!isReload && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200671 udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port);
672 shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout));
673 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700674
675 m_factories.insert(std::make_pair("udp4", factory));
676 }
677
678 if (!isReload && enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200679 udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port);
680 shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout));
681 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700682
Yanbiao Li73860e32015-08-19 16:30:16 -0700683 m_factories.insert(std::make_pair("udp6", factory));
684 }
685
Junxiao Shicde37ad2015-12-24 01:02:05 -0700686 std::set<shared_ptr<Face>> multicastFacesToRemove;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200687 for (const auto& i : factory->getMulticastFaces()) {
688 multicastFacesToRemove.insert(i.second);
689 }
690
Yanbiao Li73860e32015-08-19 16:30:16 -0700691 if (useMcast && enableV4) {
692 std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces;
693 for (const auto& nic : nicList) {
694 if (nic.isUp() && nic.isMulticastCapable() && !nic.ipv4Addresses.empty()) {
695 ipv4MulticastInterfaces.push_back(nic);
696 }
697 }
698
699 bool isNicNameNecessary = false;
700#if defined(__linux__)
701 if (ipv4MulticastInterfaces.size() > 1) {
702 // On Linux if we have more than one MulticastUdpFace
703 // we need to specify the name of the interface
704 isNicNameNecessary = true;
705 }
706#endif
707
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200708 udp::Endpoint mcastEndpoint(mcastGroup, mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700709 for (const auto& nic : ipv4MulticastInterfaces) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200710 udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort);
711 auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint,
Yukai Tu0a49d342015-09-13 12:54:22 +0800712 isNicNameNecessary ? nic.name : "");
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200713 m_faceTable.add(newFace);
714 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700715 }
716 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700717
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200718 for (const auto& face : multicastFacesToRemove) {
719 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700720 }
721 }
722}
723
724void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200725FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700726 const std::vector<NetworkInterfaceInfo>& nicList)
727{
728 // ; the ether section contains settings of Ethernet faces and channels
729 // ether
730 // {
731 // ; NFD creates one Ethernet multicast face per NIC
732 // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes'
733 // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group
734 // }
735
736#if defined(HAVE_LIBPCAP)
737 bool useMcast = true;
738 ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress());
739
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200740 for (const auto& i : configSection) {
741 if (i.first == "mcast") {
742 useMcast = ConfigFile::parseYesNo(i, "ether");
Yanbiao Li73860e32015-08-19 16:30:16 -0700743 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200744 else if (i.first == "mcast_group") {
745 mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>());
Yanbiao Li73860e32015-08-19 16:30:16 -0700746 if (mcastGroup.isNull()) {
747 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200748 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700749 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200750 NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700751 }
752 else {
753 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200754 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700755 }
756 }
757
758 if (!isDryRun) {
759 shared_ptr<EthernetFactory> factory;
760 if (m_factories.count("ether") > 0) {
761 factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]);
762 }
763 else {
764 factory = make_shared<EthernetFactory>();
765 m_factories.insert(std::make_pair("ether", factory));
766 }
767
Junxiao Shicde37ad2015-12-24 01:02:05 -0700768 std::set<shared_ptr<Face>> multicastFacesToRemove;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200769 for (const auto& i : factory->getMulticastFaces()) {
770 multicastFacesToRemove.insert(i.second);
771 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700772
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200773 if (useMcast) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700774 for (const auto& nic : nicList) {
775 if (nic.isUp() && nic.isMulticastCapable()) {
776 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200777 auto newFace = factory->createMulticastFace(nic, mcastGroup);
778 m_faceTable.add(newFace);
779 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700780 }
781 catch (const EthernetFactory::Error& factoryError) {
782 NFD_LOG_ERROR(factoryError.what() << ", continuing");
783 }
Davide Pesavento35120ea2015-11-17 21:13:18 +0100784 catch (const face::EthernetTransport::Error& faceError) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700785 NFD_LOG_ERROR(faceError.what() << ", continuing");
786 }
787 }
788 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700789 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700790
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200791 for (const auto& face : multicastFacesToRemove) {
792 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700793 }
794 }
795#else
796 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section"));
797#endif // HAVE_LIBPCAP
798}
799
800void
801FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun)
802{
803 // ; the websocket section contains settings of WebSocket faces and channels
804 // websocket
805 // {
806 // listen yes ; set to 'no' to disable WebSocket listener, default 'yes'
807 // port 9696 ; WebSocket listener port number
808 // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes'
809 // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes'
810 // }
811
812#if defined(HAVE_WEBSOCKET)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200813 uint16_t port = 9696;
Yanbiao Li73860e32015-08-19 16:30:16 -0700814 bool needToListen = true;
815 bool enableV4 = true;
816 bool enableV6 = true;
817
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200818 for (const auto& i : configSection) {
819 if (i.first == "port") {
820 port = ConfigFile::parseNumber<uint16_t>(i, "websocket");
821 NFD_LOG_TRACE("WebSocket port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700822 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200823 else if (i.first == "listen") {
824 needToListen = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700825 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200826 else if (i.first == "enable_v4") {
827 enableV4 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700828 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200829 else if (i.first == "enable_v6") {
830 enableV6 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700831 }
832 else {
833 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200834 i.first + "\" in \"websocket\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700835 }
836 }
837
838 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200839 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 WebSocket channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700840 " Remove \"websocket\" section to disable WebSocket channels or"
841 " re-enable at least one channel type."));
842 }
843
844 if (!enableV4 && enableV6) {
845 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel."));
846 }
847
848 if (!isDryRun) {
849 if (m_factories.count("websocket") > 0) {
850 return;
851 }
852
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200853 auto factory = make_shared<WebSocketFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700854 m_factories.insert(std::make_pair("websocket", factory));
855
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200856 shared_ptr<WebSocketChannel> channel;
857
Yanbiao Li73860e32015-08-19 16:30:16 -0700858 if (enableV6 && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200859 websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port);
860 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700861
862 m_factories.insert(std::make_pair("websocket46", factory));
863 }
864 else if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200865 websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port);
866 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700867
868 m_factories.insert(std::make_pair("websocket4", factory));
869 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200870
871 if (channel && needToListen) {
872 channel->listen(bind(&FaceTable::add, &m_faceTable, _1));
873 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700874 }
875#else
876 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, "
877 "cannot process \"websocket\" section"));
878#endif // HAVE_WEBSOCKET
879}
880
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200881} // namespace nfd