blob: b8506ac9a8f14729e3223a1c0aa17b916df75452 [file] [log] [blame]
Yanbiao Li73860e32015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Lidf846e52016-01-30 21:53:47 -08003 * Copyright (c) 2014-2016, Regents of the University of California,
Yanbiao Li73860e32015-08-19 16:30:16 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "face-manager.hpp"
27
28#include "core/network-interface.hpp"
Junxiao Shi40cb61c2015-09-23 18:36:45 -070029#include "face/generic-link-service.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070030#include "face/tcp-factory.hpp"
31#include "face/udp-factory.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080032#include "fw/face-table.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070033
Yanbiao Li73860e32015-08-19 16:30:16 -070034#include <ndn-cxx/management/nfd-channel-status.hpp>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020035#include <ndn-cxx/management/nfd-face-status.hpp>
Yanbiao Li73860e32015-08-19 16:30:16 -070036#include <ndn-cxx/management/nfd-face-event-notification.hpp>
37
38#ifdef HAVE_UNIX_SOCKETS
39#include "face/unix-stream-factory.hpp"
40#endif // HAVE_UNIX_SOCKETS
41
42#ifdef HAVE_LIBPCAP
43#include "face/ethernet-factory.hpp"
Davide Pesavento35120ea2015-11-17 21:13:18 +010044#include "face/ethernet-transport.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070045#endif // HAVE_LIBPCAP
46
47#ifdef HAVE_WEBSOCKET
48#include "face/websocket-factory.hpp"
49#endif // HAVE_WEBSOCKET
50
51namespace nfd {
52
53NFD_LOG_INIT("FaceManager");
54
55FaceManager::FaceManager(FaceTable& faceTable,
56 Dispatcher& dispatcher,
57 CommandValidator& validator)
Yanbiao Lidf846e52016-01-30 21:53:47 -080058 : NfdManagerBase(dispatcher, validator, "faces")
Yanbiao Li73860e32015-08-19 16:30:16 -070059 , 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{
Eric Newberryc64d30a2015-12-26 11:07:27 -0700251 auto now = time::steady_clock::now();
Yanbiao Li73860e32015-08-19 16:30:16 -0700252 for (const auto& face : m_faceTable) {
Eric Newberryc64d30a2015-12-26 11:07:27 -0700253 ndn::nfd::FaceStatus status = collectFaceStatus(*face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700254 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700255 }
256 context.end();
257}
258
259void
260FaceManager::listChannels(const Name& topPrefix, const Interest& interest,
261 ndn::mgmt::StatusDatasetContext& context)
262{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200263 std::set<const ProtocolFactory*> seenFactories;
Yanbiao Li73860e32015-08-19 16:30:16 -0700264
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200265 for (const auto& kv : m_factories) {
266 const ProtocolFactory* factory = kv.second.get();
267 bool inserted;
268 std::tie(std::ignore, inserted) = seenFactories.insert(factory);
Yanbiao Li73860e32015-08-19 16:30:16 -0700269
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200270 if (inserted) {
271 for (const auto& channel : factory->getChannels()) {
272 ndn::nfd::ChannelStatus entry;
273 entry.setLocalUri(channel->getUri().toString());
274 context.append(entry.wireEncode());
275 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700276 }
277 }
278
279 context.end();
280}
281
282void
283FaceManager::queryFaces(const Name& topPrefix, const Interest& interest,
284 ndn::mgmt::StatusDatasetContext& context)
285{
286 ndn::nfd::FaceQueryFilter faceFilter;
287 const Name& query = interest.getName();
288 try {
289 faceFilter.wireDecode(query[-1].blockFromValue());
290 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200291 catch (const tlv::Error& e) {
292 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
293 return context.reject(ControlResponse(400, "Malformed filter"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700294 }
295
Eric Newberryc64d30a2015-12-26 11:07:27 -0700296 auto now = time::steady_clock::now();
Yanbiao Li73860e32015-08-19 16:30:16 -0700297 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 }
Eric Newberryc64d30a2015-12-26 11:07:27 -0700301 ndn::nfd::FaceStatus status = collectFaceStatus(*face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700302 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700303 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200304
Yanbiao Li73860e32015-08-19 16:30:16 -0700305 context.end();
306}
307
308bool
309FaceManager::doesMatchFilter(const ndn::nfd::FaceQueryFilter& filter, shared_ptr<Face> face)
310{
311 if (filter.hasFaceId() &&
312 filter.getFaceId() != static_cast<uint64_t>(face->getId())) {
313 return false;
314 }
315
316 if (filter.hasUriScheme() &&
317 filter.getUriScheme() != face->getRemoteUri().getScheme() &&
318 filter.getUriScheme() != face->getLocalUri().getScheme()) {
319 return false;
320 }
321
322 if (filter.hasRemoteUri() &&
323 filter.getRemoteUri() != face->getRemoteUri().toString()) {
324 return false;
325 }
326
327 if (filter.hasLocalUri() &&
328 filter.getLocalUri() != face->getLocalUri().toString()) {
329 return false;
330 }
331
332 if (filter.hasFaceScope() &&
Junxiao Shicde37ad2015-12-24 01:02:05 -0700333 filter.getFaceScope() != face->getScope()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700334 return false;
335 }
336
337 if (filter.hasFacePersistency() &&
338 filter.getFacePersistency() != face->getPersistency()) {
339 return false;
340 }
341
342 if (filter.hasLinkType() &&
Junxiao Shicde37ad2015-12-24 01:02:05 -0700343 filter.getLinkType() != face->getLinkType()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700344 return false;
345 }
346
347 return true;
348}
349
Eric Newberryc64d30a2015-12-26 11:07:27 -0700350ndn::nfd::FaceStatus
351FaceManager::collectFaceStatus(const Face& face, const time::steady_clock::TimePoint& now)
352{
353 ndn::nfd::FaceStatus status;
354
355 collectFaceProperties(face, status);
356
357 time::steady_clock::TimePoint expirationTime = face.getExpirationTime();
358 if (expirationTime != time::steady_clock::TimePoint::max()) {
359 status.setExpirationPeriod(std::max(time::milliseconds(0),
360 time::duration_cast<time::milliseconds>(expirationTime - now)));
361 }
362
363 const face::FaceCounters& counters = face.getCounters();
364 status.setNInInterests(counters.nInInterests)
365 .setNOutInterests(counters.nOutInterests)
366 .setNInDatas(counters.nInData)
367 .setNOutDatas(counters.nOutData)
368 .setNInNacks(counters.nInNacks)
369 .setNOutNacks(counters.nOutNacks)
370 .setNInBytes(counters.nInBytes)
371 .setNOutBytes(counters.nOutBytes);
372
373 return status;
374}
375
Junxiao Shida93f1f2015-11-11 06:13:16 -0700376template<typename FaceTraits>
Junxiao Shicde37ad2015-12-24 01:02:05 -0700377void
378FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits)
Junxiao Shida93f1f2015-11-11 06:13:16 -0700379{
380 traits.setFaceId(face.getId())
381 .setRemoteUri(face.getRemoteUri().toString())
382 .setLocalUri(face.getLocalUri().toString())
383 .setFaceScope(face.getScope())
384 .setFacePersistency(face.getPersistency())
385 .setLinkType(face.getLinkType());
Junxiao Shida93f1f2015-11-11 06:13:16 -0700386}
387
388void
Yanbiao Li73860e32015-08-19 16:30:16 -0700389FaceManager::afterFaceAdded(shared_ptr<Face> face,
390 const ndn::mgmt::PostNotification& post)
391{
392 ndn::nfd::FaceEventNotification notification;
393 notification.setKind(ndn::nfd::FACE_EVENT_CREATED);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700394 collectFaceProperties(*face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700395
396 post(notification.wireEncode());
397}
398
399void
400FaceManager::afterFaceRemoved(shared_ptr<Face> face,
401 const ndn::mgmt::PostNotification& post)
402{
403 ndn::nfd::FaceEventNotification notification;
404 notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700405 collectFaceProperties(*face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700406
407 post(notification.wireEncode());
408}
409
410void
411FaceManager::processConfig(const ConfigSection& configSection,
412 bool isDryRun,
413 const std::string& filename)
414{
415 bool hasSeenUnix = false;
416 bool hasSeenTcp = false;
417 bool hasSeenUdp = false;
418 bool hasSeenEther = false;
419 bool hasSeenWebSocket = false;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200420 auto nicList = listNetworkInterfaces();
Yanbiao Li73860e32015-08-19 16:30:16 -0700421
422 for (const auto& item : configSection) {
423 if (item.first == "unix") {
424 if (hasSeenUnix) {
425 BOOST_THROW_EXCEPTION(Error("Duplicate \"unix\" section"));
426 }
427 hasSeenUnix = true;
428
429 processSectionUnix(item.second, isDryRun);
430 }
431 else if (item.first == "tcp") {
432 if (hasSeenTcp) {
433 BOOST_THROW_EXCEPTION(Error("Duplicate \"tcp\" section"));
434 }
435 hasSeenTcp = true;
436
437 processSectionTcp(item.second, isDryRun);
438 }
439 else if (item.first == "udp") {
440 if (hasSeenUdp) {
441 BOOST_THROW_EXCEPTION(Error("Duplicate \"udp\" section"));
442 }
443 hasSeenUdp = true;
444
445 processSectionUdp(item.second, isDryRun, nicList);
446 }
447 else if (item.first == "ether") {
448 if (hasSeenEther) {
449 BOOST_THROW_EXCEPTION(Error("Duplicate \"ether\" section"));
450 }
451 hasSeenEther = true;
452
453 processSectionEther(item.second, isDryRun, nicList);
454 }
455 else if (item.first == "websocket") {
456 if (hasSeenWebSocket) {
457 BOOST_THROW_EXCEPTION(Error("Duplicate \"websocket\" section"));
458 }
459 hasSeenWebSocket = true;
460
461 processSectionWebSocket(item.second, isDryRun);
462 }
463 else {
464 BOOST_THROW_EXCEPTION(Error("Unrecognized option \"" + item.first + "\""));
465 }
466 }
467}
468
469void
470FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun)
471{
472 // ; the unix section contains settings of Unix stream faces and channels
473 // unix
474 // {
475 // path /var/run/nfd.sock ; Unix stream listener path
476 // }
477
478#if defined(HAVE_UNIX_SOCKETS)
Yanbiao Li73860e32015-08-19 16:30:16 -0700479 std::string path = "/var/run/nfd.sock";
480
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200481 for (const auto& i : configSection) {
482 if (i.first == "path") {
483 path = i.second.get_value<std::string>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700484 }
485 else {
486 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200487 i.first + "\" in \"unix\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700488 }
489 }
490
491 if (!isDryRun) {
492 if (m_factories.count("unix") > 0) {
493 return;
494 }
495
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200496 auto factory = make_shared<UnixStreamFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700497 m_factories.insert(std::make_pair("unix", factory));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200498
499 auto channel = factory->createChannel(path);
500 channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700501 }
502#else
503 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, "
504 "cannot process \"unix\" section"));
505#endif // HAVE_UNIX_SOCKETS
506}
507
508void
509FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun)
510{
511 // ; the tcp section contains settings of TCP faces and channels
512 // tcp
513 // {
514 // listen yes ; set to 'no' to disable TCP listener, default 'yes'
515 // port 6363 ; TCP listener port number
516 // }
517
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200518 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700519 bool needToListen = true;
520 bool enableV4 = true;
521 bool enableV6 = true;
522
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200523 for (const auto& i : configSection) {
524 if (i.first == "port") {
525 port = ConfigFile::parseNumber<uint16_t>(i, "tcp");
526 NFD_LOG_TRACE("TCP port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700527 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200528 else if (i.first == "listen") {
529 needToListen = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700530 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200531 else if (i.first == "enable_v4") {
532 enableV4 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700533 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200534 else if (i.first == "enable_v6") {
535 enableV6 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700536 }
537 else {
538 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200539 i.first + "\" in \"tcp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700540 }
541 }
542
543 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200544 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 TCP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700545 " Remove \"tcp\" section to disable TCP channels or"
546 " re-enable at least one channel type."));
547 }
548
549 if (!isDryRun) {
550 if (m_factories.count("tcp") > 0) {
551 return;
552 }
553
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200554 auto factory = make_shared<TcpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700555 m_factories.insert(std::make_pair("tcp", factory));
556
557 if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200558 tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port);
559 shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700560 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200561 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700562 }
563
564 m_factories.insert(std::make_pair("tcp4", factory));
565 }
566
567 if (enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200568 tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port);
569 shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700570 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200571 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700572 }
573
574 m_factories.insert(std::make_pair("tcp6", factory));
575 }
576 }
577}
578
579void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200580FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700581 const std::vector<NetworkInterfaceInfo>& nicList)
582{
583 // ; the udp section contains settings of UDP faces and channels
584 // udp
585 // {
586 // port 6363 ; UDP unicast port number
587 // idle_timeout 600 ; idle time (seconds) before closing a UDP unicast face
588 // keep_alive_interval 25 ; interval (seconds) between keep-alive refreshes
589
590 // ; NFD creates one UDP multicast face per NIC
591 // mcast yes ; set to 'no' to disable UDP multicast, default 'yes'
592 // mcast_port 56363 ; UDP multicast port number
593 // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only)
594 // }
595
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200596 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700597 bool enableV4 = true;
598 bool enableV6 = true;
599 size_t timeout = 600;
600 size_t keepAliveInterval = 25;
601 bool useMcast = true;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200602 auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170");
603 uint16_t mcastPort = 56363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700604
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200605 for (const auto& i : configSection) {
606 if (i.first == "port") {
607 port = ConfigFile::parseNumber<uint16_t>(i, "udp");
608 NFD_LOG_TRACE("UDP unicast port set to " << port);
609 }
610 else if (i.first == "enable_v4") {
611 enableV4 = ConfigFile::parseYesNo(i, "udp");
612 }
613 else if (i.first == "enable_v6") {
614 enableV6 = ConfigFile::parseYesNo(i, "udp");
615 }
616 else if (i.first == "idle_timeout") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700617 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200618 timeout = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700619 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200620 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700621 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200622 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700623 }
624 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200625 else if (i.first == "keep_alive_interval") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700626 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200627 keepAliveInterval = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700628 /// \todo Make use of keepAliveInterval
Yanbiao Li73860e32015-08-19 16:30:16 -0700629 (void)(keepAliveInterval);
630 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200631 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700632 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200633 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700634 }
635 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200636 else if (i.first == "mcast") {
637 useMcast = ConfigFile::parseYesNo(i, "udp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700638 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200639 else if (i.first == "mcast_port") {
640 mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp");
641 NFD_LOG_TRACE("UDP multicast port set to " << mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700642 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200643 else if (i.first == "mcast_group") {
644 boost::system::error_code ec;
645 mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec);
646 if (ec) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700647 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200648 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700649 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200650 NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700651 }
652 else {
653 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200654 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700655 }
656 }
657
658 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200659 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 UDP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700660 " Remove \"udp\" section to disable UDP channels or"
661 " re-enable at least one channel type."));
662 }
663 else if (useMcast && !enableV4) {
664 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 multicast requested, but IPv4 channels"
665 " have been disabled (conflicting configuration options set)"));
666 }
667
668 if (!isDryRun) {
669 shared_ptr<UdpFactory> factory;
670 bool isReload = false;
671 if (m_factories.count("udp") > 0) {
672 isReload = true;
673 factory = static_pointer_cast<UdpFactory>(m_factories["udp"]);
674 }
675 else {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200676 factory = make_shared<UdpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700677 m_factories.insert(std::make_pair("udp", factory));
678 }
679
680 if (!isReload && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200681 udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port);
682 shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout));
683 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700684
685 m_factories.insert(std::make_pair("udp4", factory));
686 }
687
688 if (!isReload && enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200689 udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port);
690 shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout));
691 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700692
Yanbiao Li73860e32015-08-19 16:30:16 -0700693 m_factories.insert(std::make_pair("udp6", factory));
694 }
695
Junxiao Shicde37ad2015-12-24 01:02:05 -0700696 std::set<shared_ptr<Face>> multicastFacesToRemove;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200697 for (const auto& i : factory->getMulticastFaces()) {
698 multicastFacesToRemove.insert(i.second);
699 }
700
Yanbiao Li73860e32015-08-19 16:30:16 -0700701 if (useMcast && enableV4) {
702 std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces;
703 for (const auto& nic : nicList) {
704 if (nic.isUp() && nic.isMulticastCapable() && !nic.ipv4Addresses.empty()) {
705 ipv4MulticastInterfaces.push_back(nic);
706 }
707 }
708
709 bool isNicNameNecessary = false;
710#if defined(__linux__)
711 if (ipv4MulticastInterfaces.size() > 1) {
712 // On Linux if we have more than one MulticastUdpFace
713 // we need to specify the name of the interface
714 isNicNameNecessary = true;
715 }
716#endif
717
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200718 udp::Endpoint mcastEndpoint(mcastGroup, mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700719 for (const auto& nic : ipv4MulticastInterfaces) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200720 udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort);
721 auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint,
Yukai Tu0a49d342015-09-13 12:54:22 +0800722 isNicNameNecessary ? nic.name : "");
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200723 m_faceTable.add(newFace);
724 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700725 }
726 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700727
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200728 for (const auto& face : multicastFacesToRemove) {
729 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700730 }
731 }
732}
733
734void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200735FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700736 const std::vector<NetworkInterfaceInfo>& nicList)
737{
738 // ; the ether section contains settings of Ethernet faces and channels
739 // ether
740 // {
741 // ; NFD creates one Ethernet multicast face per NIC
742 // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes'
743 // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group
744 // }
745
746#if defined(HAVE_LIBPCAP)
747 bool useMcast = true;
748 ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress());
749
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200750 for (const auto& i : configSection) {
751 if (i.first == "mcast") {
752 useMcast = ConfigFile::parseYesNo(i, "ether");
Yanbiao Li73860e32015-08-19 16:30:16 -0700753 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200754 else if (i.first == "mcast_group") {
755 mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>());
Yanbiao Li73860e32015-08-19 16:30:16 -0700756 if (mcastGroup.isNull()) {
757 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200758 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700759 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200760 NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700761 }
762 else {
763 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200764 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700765 }
766 }
767
768 if (!isDryRun) {
769 shared_ptr<EthernetFactory> factory;
770 if (m_factories.count("ether") > 0) {
771 factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]);
772 }
773 else {
774 factory = make_shared<EthernetFactory>();
775 m_factories.insert(std::make_pair("ether", factory));
776 }
777
Junxiao Shicde37ad2015-12-24 01:02:05 -0700778 std::set<shared_ptr<Face>> multicastFacesToRemove;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200779 for (const auto& i : factory->getMulticastFaces()) {
780 multicastFacesToRemove.insert(i.second);
781 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700782
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200783 if (useMcast) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700784 for (const auto& nic : nicList) {
785 if (nic.isUp() && nic.isMulticastCapable()) {
786 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200787 auto newFace = factory->createMulticastFace(nic, mcastGroup);
788 m_faceTable.add(newFace);
789 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700790 }
791 catch (const EthernetFactory::Error& factoryError) {
792 NFD_LOG_ERROR(factoryError.what() << ", continuing");
793 }
Davide Pesavento35120ea2015-11-17 21:13:18 +0100794 catch (const face::EthernetTransport::Error& faceError) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700795 NFD_LOG_ERROR(faceError.what() << ", continuing");
796 }
797 }
798 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700799 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700800
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200801 for (const auto& face : multicastFacesToRemove) {
802 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700803 }
804 }
805#else
806 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section"));
807#endif // HAVE_LIBPCAP
808}
809
810void
811FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun)
812{
813 // ; the websocket section contains settings of WebSocket faces and channels
814 // websocket
815 // {
816 // listen yes ; set to 'no' to disable WebSocket listener, default 'yes'
817 // port 9696 ; WebSocket listener port number
818 // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes'
819 // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes'
820 // }
821
822#if defined(HAVE_WEBSOCKET)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200823 uint16_t port = 9696;
Yanbiao Li73860e32015-08-19 16:30:16 -0700824 bool needToListen = true;
825 bool enableV4 = true;
826 bool enableV6 = true;
827
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200828 for (const auto& i : configSection) {
829 if (i.first == "port") {
830 port = ConfigFile::parseNumber<uint16_t>(i, "websocket");
831 NFD_LOG_TRACE("WebSocket port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700832 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200833 else if (i.first == "listen") {
834 needToListen = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700835 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200836 else if (i.first == "enable_v4") {
837 enableV4 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700838 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200839 else if (i.first == "enable_v6") {
840 enableV6 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700841 }
842 else {
843 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200844 i.first + "\" in \"websocket\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700845 }
846 }
847
848 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200849 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 WebSocket channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700850 " Remove \"websocket\" section to disable WebSocket channels or"
851 " re-enable at least one channel type."));
852 }
853
854 if (!enableV4 && enableV6) {
855 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel."));
856 }
857
858 if (!isDryRun) {
859 if (m_factories.count("websocket") > 0) {
860 return;
861 }
862
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200863 auto factory = make_shared<WebSocketFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700864 m_factories.insert(std::make_pair("websocket", factory));
865
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200866 shared_ptr<WebSocketChannel> channel;
867
Yanbiao Li73860e32015-08-19 16:30:16 -0700868 if (enableV6 && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200869 websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port);
870 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700871
872 m_factories.insert(std::make_pair("websocket46", factory));
873 }
874 else if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200875 websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port);
876 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700877
878 m_factories.insert(std::make_pair("websocket4", factory));
879 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200880
881 if (channel && needToListen) {
882 channel->listen(bind(&FaceTable::add, &m_faceTable, _1));
883 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700884 }
885#else
886 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, "
887 "cannot process \"websocket\" section"));
888#endif // HAVE_WEBSOCKET
889}
890
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200891} // namespace nfd