blob: c03f9d8dfab3cdf476db8579beba0ed95e48827c [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
Junxiao Shicbc8e942016-09-06 03:17:45 +000034#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000035#include <ndn-cxx/mgmt/nfd/channel-status.hpp>
36#include <ndn-cxx/mgmt/nfd/face-status.hpp>
37#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
Yanbiao Li73860e32015-08-19 16:30:16 -070038
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
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000056FaceManager::FaceManager(FaceTable& faceTable, Dispatcher& dispatcher, CommandAuthenticator& authenticator)
57 : NfdManagerBase(dispatcher, authenticator, "faces")
Yanbiao Li73860e32015-08-19 16:30:16 -070058 , m_faceTable(faceTable)
59{
60 registerCommandHandler<ndn::nfd::FaceCreateCommand>("create",
61 bind(&FaceManager::createFace, this, _2, _3, _4, _5));
62
Eric Newberryb5aa7f52016-09-03 20:36:12 -070063 registerCommandHandler<ndn::nfd::FaceUpdateCommand>("update",
64 bind(&FaceManager::updateFace, this, _2, _3, _4, _5));
65
Yanbiao Li73860e32015-08-19 16:30:16 -070066 registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy",
67 bind(&FaceManager::destroyFace, this, _2, _3, _4, _5));
68
69 registerCommandHandler<ndn::nfd::FaceEnableLocalControlCommand>("enable-local-control",
70 bind(&FaceManager::enableLocalControl, this, _2, _3, _4, _5));
71
72 registerCommandHandler<ndn::nfd::FaceDisableLocalControlCommand>("disable-local-control",
73 bind(&FaceManager::disableLocalControl, this, _2, _3, _4, _5));
74
75 registerStatusDatasetHandler("list", bind(&FaceManager::listFaces, this, _1, _2, _3));
76 registerStatusDatasetHandler("channels", bind(&FaceManager::listChannels, this, _1, _2, _3));
77 registerStatusDatasetHandler("query", bind(&FaceManager::queryFaces, this, _1, _2, _3));
78
Junxiao Shiae04d342016-07-19 13:20:22 +000079 m_postNotification = registerNotificationStream("events");
80 m_faceAddConn = m_faceTable.afterAdd.connect(bind(&FaceManager::notifyAddFace, this, _1));
81 m_faceRemoveConn = m_faceTable.beforeRemove.connect(bind(&FaceManager::notifyRemoveFace, this, _1));
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");
Eric Newberry42602412016-08-27 09:33:18 -070098 done(ControlResponse(400, "Malformed command"));
99 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700100 }
101
102 if (!uri.isCanonical()) {
103 NFD_LOG_TRACE("received non-canonical URI");
Eric Newberry42602412016-08-27 09:33:18 -0700104 done(ControlResponse(400, "Non-canonical URI"));
105 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700106 }
107
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200108 auto factory = m_factories.find(uri.getScheme());
Yanbiao Li73860e32015-08-19 16:30:16 -0700109 if (factory == m_factories.end()) {
Eric Newberry42602412016-08-27 09:33:18 -0700110 NFD_LOG_TRACE("received create request for unsupported protocol");
111 done(ControlResponse(406, "Unsupported protocol"));
112 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700113 }
114
115 try {
116 factory->second->createFace(uri,
117 parameters.getFacePersistency(),
Eric Newberryf40551a2016-09-05 15:41:16 -0700118 parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ?
119 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) : false,
Yanbiao Li73860e32015-08-19 16:30:16 -0700120 bind(&FaceManager::afterCreateFaceSuccess,
121 this, parameters, _1, done),
122 bind(&FaceManager::afterCreateFaceFailure,
Eric Newberry42602412016-08-27 09:33:18 -0700123 this, _1, _2, done));
Yanbiao Li73860e32015-08-19 16:30:16 -0700124 }
125 catch (const std::runtime_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700126 NFD_LOG_ERROR("Face creation failed: " << error.what());
127 done(ControlResponse(500, "Face creation failed due to internal error"));
128 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700129 }
130 catch (const std::logic_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700131 NFD_LOG_ERROR("Face creation failed: " << error.what());
132 done(ControlResponse(500, "Face creation failed due to internal error"));
133 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700134 }
135}
136
Eric Newberry42602412016-08-27 09:33:18 -0700137/**
138 * \todo #3232
139 * If the creation of this face would conflict with an existing face (e.g. same underlying
140 * protocol and remote address, or a NIC-associated permanent face), the command will fail
141 * with StatusCode 409.
142 */
Yanbiao Li73860e32015-08-19 16:30:16 -0700143void
Eric Newberry42602412016-08-27 09:33:18 -0700144FaceManager::afterCreateFaceSuccess(const ControlParameters& parameters,
Eric Newberryf40551a2016-09-05 15:41:16 -0700145 const shared_ptr<Face>& face,
Yanbiao Li73860e32015-08-19 16:30:16 -0700146 const ndn::mgmt::CommandContinuation& done)
147{
Eric Newberryf40551a2016-09-05 15:41:16 -0700148 // TODO: Re-enable check in #3232
149 //if (face->getId() != face::INVALID_FACEID) {
150 //// Face already exists
151 //ControlParameters response;
152 //response.setFaceId(face->getId());
153 //response.setUri(face->getRemoteUri().toString());
154 //response.setFacePersistency(face->getPersistency());
155 //
156 //auto linkService = dynamic_cast<face::GenericLinkService*>(face->getLinkService());
157 //BOOST_ASSERT(linkService != nullptr);
158 //auto options = linkService->getOptions();
159 //response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false);
160 //
161 // NFD_LOG_TRACE("Attempted to create duplicate face of " << face->getId());
162 // done(ControlResponse(409, "Face with remote URI already exists").setBody(response.wireEncode()));
163 //}
164 //else {
165 // If scope non-local and flags set to enable local fields, request shouldn't
166 // have made it this far
167 BOOST_ASSERT(face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL ||
168 !parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ||
169 (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
170 !parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)));
171
172 m_faceTable.add(face);
173
Eric Newberry42602412016-08-27 09:33:18 -0700174 ControlParameters response;
Eric Newberryf40551a2016-09-05 15:41:16 -0700175 response.setFaceId(face->getId());
176 response.setFacePersistency(face->getPersistency());
177 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
178 parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ?
179 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) : false,
180 false);
Eric Newberry42602412016-08-27 09:33:18 -0700181
182 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
Eric Newberryf40551a2016-09-05 15:41:16 -0700183 //}
Eric Newberry42602412016-08-27 09:33:18 -0700184}
185
186void
187FaceManager::afterCreateFaceFailure(uint32_t status,
188 const std::string& reason,
189 const ndn::mgmt::CommandContinuation& done)
190{
191 NFD_LOG_DEBUG("Face creation failed: " << reason);
192
193 done(ControlResponse(status, reason));
Yanbiao Li73860e32015-08-19 16:30:16 -0700194}
195
196void
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700197FaceManager::updateFace(const Name& topPrefix, const Interest& interest,
198 const ControlParameters& parameters,
199 const ndn::mgmt::CommandContinuation& done)
200{
201 FaceId faceId = parameters.getFaceId();
202 if (faceId == 0) {
203 // Self-updating
204 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>();
205 if (incomingFaceIdTag == nullptr) {
206 NFD_LOG_TRACE("unable to determine face for self-update");
207 done(ControlResponse(404, "No FaceId specified and IncomingFaceId not available"));
208 return;
209 }
210 faceId = *incomingFaceIdTag;
211 }
212
213 Face* face = m_faceTable.get(faceId);
214
215 if (face == nullptr) {
216 NFD_LOG_TRACE("invalid face specified");
217 done(ControlResponse(404, "Specified face does not exist"));
218 return;
219 }
220
221 // Verify validity of requested changes
222 ControlParameters response;
223 bool areParamsValid = true;
224
225 if (parameters.hasFacePersistency()) {
226 // TODO #3232: Add FacePersistency updating
227 NFD_LOG_TRACE("received unsupported face persistency change");
228 areParamsValid = false;
229 response.setFacePersistency(parameters.getFacePersistency());
230 }
231
232 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
233 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
234 face->getScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
235 NFD_LOG_TRACE("received request to enable local fields on non-local face");
236 areParamsValid = false;
237 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
238 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
239 }
240
241 if (!areParamsValid) {
242 done(ControlResponse(409, "Invalid properties specified").setBody(response.wireEncode()));
243 return;
244 }
245
246 // All specified properties are valid, so make changes
247
248 // TODO #3232: Add FacePersistency updating
249
250 setLinkServiceOptions(*face, parameters, response);
251
Eric Newberryf40551a2016-09-05 15:41:16 -0700252 // Set ControlResponse fields
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700253 response.setFaceId(faceId);
254 response.setFacePersistency(face->getPersistency());
Eric Newberryf40551a2016-09-05 15:41:16 -0700255 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
256 parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ?
257 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) : false,
258 false);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700259
260 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
261}
262
263void
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700264FaceManager::destroyFace(const Name& topPrefix, const Interest& interest,
265 const ControlParameters& parameters,
266 const ndn::mgmt::CommandContinuation& done)
267{
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000268 Face* face = m_faceTable.get(parameters.getFaceId());
269 if (face != nullptr) {
270 face->close();
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700271 }
272
273 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
274}
275
276void
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700277FaceManager::enableLocalControl(const Name& topPrefix, const Interest& interest,
278 const ControlParameters& parameters,
279 const ndn::mgmt::CommandContinuation& done)
280{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700281 Face* face = findFaceForLocalControl(interest, parameters, done);
282 if (!face) {
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700283 return;
284 }
285
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700286 // enable-local-control will enable all local fields in GenericLinkService
Junxiao Shicde37ad2015-12-24 01:02:05 -0700287 auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService());
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700288 if (service == nullptr) {
289 return done(ControlResponse(503, "LinkService type not supported"));
290 }
291
292 face::GenericLinkService::Options options = service->getOptions();
293 options.allowLocalFields = true;
294 service->setOptions(options);
295
296 return done(ControlResponse(200, "OK: enable all local fields on GenericLinkService")
297 .setBody(parameters.wireEncode()));
298}
299
300void
301FaceManager::disableLocalControl(const Name& topPrefix, const Interest& interest,
302 const ControlParameters& parameters,
303 const ndn::mgmt::CommandContinuation& done)
304{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700305 Face* face = findFaceForLocalControl(interest, parameters, done);
306 if (!face) {
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700307 return;
308 }
309
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700310 // disable-local-control will disable all local fields in GenericLinkService
Junxiao Shicde37ad2015-12-24 01:02:05 -0700311 auto service = dynamic_cast<face::GenericLinkService*>(face->getLinkService());
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700312 if (service == nullptr) {
313 return done(ControlResponse(503, "LinkService type not supported"));
314 }
315
316 face::GenericLinkService::Options options = service->getOptions();
317 options.allowLocalFields = false;
318 service->setOptions(options);
319
320 return done(ControlResponse(200, "OK: disable all local fields on GenericLinkService")
321 .setBody(parameters.wireEncode()));
322}
323
Junxiao Shicde37ad2015-12-24 01:02:05 -0700324Face*
325FaceManager::findFaceForLocalControl(const Interest& request,
326 const ControlParameters& parameters,
327 const ndn::mgmt::CommandContinuation& done)
Yanbiao Li73860e32015-08-19 16:30:16 -0700328{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000329 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
330 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
331 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
332 // and is initialized synchronously with IncomingFaceId field enabled.
333 BOOST_ASSERT(incomingFaceIdTag != nullptr);
334
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000335 Face* face = m_faceTable.get(*incomingFaceIdTag);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200336 if (face == nullptr) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000337 NFD_LOG_DEBUG("FaceId " << *incomingFaceIdTag << " not found");
Yanbiao Li73860e32015-08-19 16:30:16 -0700338 done(ControlResponse(410, "Face not found"));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700339 return nullptr;
Yanbiao Li73860e32015-08-19 16:30:16 -0700340 }
341
Junxiao Shicde37ad2015-12-24 01:02:05 -0700342 if (face->getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200343 NFD_LOG_DEBUG("Cannot enable local control on non-local FaceId " << face->getId());
Yanbiao Li73860e32015-08-19 16:30:16 -0700344 done(ControlResponse(412, "Face is non-local"));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700345 return nullptr;
Yanbiao Li73860e32015-08-19 16:30:16 -0700346 }
347
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000348 return face;
Yanbiao Li73860e32015-08-19 16:30:16 -0700349}
350
351void
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700352FaceManager::setLinkServiceOptions(Face& face,
353 const ControlParameters& parameters,
354 ControlParameters& response)
355{
356 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
357 BOOST_ASSERT(linkService != nullptr);
358
359 auto options = linkService->getOptions();
360 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
361 face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
362 options.allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
363 }
364 linkService->setOptions(options);
365
366 // Set Flags for ControlResponse
367 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false);
368}
369
370void
Yanbiao Li73860e32015-08-19 16:30:16 -0700371FaceManager::listFaces(const Name& topPrefix, const Interest& interest,
372 ndn::mgmt::StatusDatasetContext& context)
373{
Eric Newberryc64d30a2015-12-26 11:07:27 -0700374 auto now = time::steady_clock::now();
Junxiao Shib84e6742016-07-19 13:16:22 +0000375 for (const Face& face : m_faceTable) {
376 ndn::nfd::FaceStatus status = collectFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700377 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700378 }
379 context.end();
380}
381
382void
383FaceManager::listChannels(const Name& topPrefix, const Interest& interest,
384 ndn::mgmt::StatusDatasetContext& context)
385{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200386 std::set<const ProtocolFactory*> seenFactories;
Yanbiao Li73860e32015-08-19 16:30:16 -0700387
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200388 for (const auto& kv : m_factories) {
389 const ProtocolFactory* factory = kv.second.get();
390 bool inserted;
391 std::tie(std::ignore, inserted) = seenFactories.insert(factory);
Yanbiao Li73860e32015-08-19 16:30:16 -0700392
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200393 if (inserted) {
394 for (const auto& channel : factory->getChannels()) {
395 ndn::nfd::ChannelStatus entry;
396 entry.setLocalUri(channel->getUri().toString());
397 context.append(entry.wireEncode());
398 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700399 }
400 }
401
402 context.end();
403}
404
405void
406FaceManager::queryFaces(const Name& topPrefix, const Interest& interest,
407 ndn::mgmt::StatusDatasetContext& context)
408{
409 ndn::nfd::FaceQueryFilter faceFilter;
410 const Name& query = interest.getName();
411 try {
412 faceFilter.wireDecode(query[-1].blockFromValue());
413 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200414 catch (const tlv::Error& e) {
415 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
416 return context.reject(ControlResponse(400, "Malformed filter"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700417 }
418
Eric Newberryc64d30a2015-12-26 11:07:27 -0700419 auto now = time::steady_clock::now();
Junxiao Shib84e6742016-07-19 13:16:22 +0000420 for (const Face& face : m_faceTable) {
421 if (!matchFilter(faceFilter, face)) {
Junxiao Shida93f1f2015-11-11 06:13:16 -0700422 continue;
Yanbiao Li73860e32015-08-19 16:30:16 -0700423 }
Junxiao Shib84e6742016-07-19 13:16:22 +0000424 ndn::nfd::FaceStatus status = collectFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700425 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700426 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200427
Yanbiao Li73860e32015-08-19 16:30:16 -0700428 context.end();
429}
430
431bool
Junxiao Shib84e6742016-07-19 13:16:22 +0000432FaceManager::matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700433{
434 if (filter.hasFaceId() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000435 filter.getFaceId() != static_cast<uint64_t>(face.getId())) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700436 return false;
437 }
438
439 if (filter.hasUriScheme() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000440 filter.getUriScheme() != face.getRemoteUri().getScheme() &&
441 filter.getUriScheme() != face.getLocalUri().getScheme()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700442 return false;
443 }
444
445 if (filter.hasRemoteUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000446 filter.getRemoteUri() != face.getRemoteUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700447 return false;
448 }
449
450 if (filter.hasLocalUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000451 filter.getLocalUri() != face.getLocalUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700452 return false;
453 }
454
455 if (filter.hasFaceScope() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000456 filter.getFaceScope() != face.getScope()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700457 return false;
458 }
459
460 if (filter.hasFacePersistency() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000461 filter.getFacePersistency() != face.getPersistency()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700462 return false;
463 }
464
465 if (filter.hasLinkType() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000466 filter.getLinkType() != face.getLinkType()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700467 return false;
468 }
469
470 return true;
471}
472
Eric Newberryc64d30a2015-12-26 11:07:27 -0700473ndn::nfd::FaceStatus
474FaceManager::collectFaceStatus(const Face& face, const time::steady_clock::TimePoint& now)
475{
476 ndn::nfd::FaceStatus status;
477
478 collectFaceProperties(face, status);
479
480 time::steady_clock::TimePoint expirationTime = face.getExpirationTime();
481 if (expirationTime != time::steady_clock::TimePoint::max()) {
482 status.setExpirationPeriod(std::max(time::milliseconds(0),
483 time::duration_cast<time::milliseconds>(expirationTime - now)));
484 }
485
486 const face::FaceCounters& counters = face.getCounters();
487 status.setNInInterests(counters.nInInterests)
488 .setNOutInterests(counters.nOutInterests)
489 .setNInDatas(counters.nInData)
490 .setNOutDatas(counters.nOutData)
491 .setNInNacks(counters.nInNacks)
492 .setNOutNacks(counters.nOutNacks)
493 .setNInBytes(counters.nInBytes)
494 .setNOutBytes(counters.nOutBytes);
495
Eric Newberryc05918c2016-09-29 10:38:13 -0700496 // Set Flag bits
497 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
498 if (linkService != nullptr) {
499 auto linkServiceOptions = linkService->getOptions();
500 status.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, linkServiceOptions.allowLocalFields);
501 }
502
Eric Newberryc64d30a2015-12-26 11:07:27 -0700503 return status;
504}
505
Junxiao Shida93f1f2015-11-11 06:13:16 -0700506template<typename FaceTraits>
Junxiao Shicde37ad2015-12-24 01:02:05 -0700507void
508FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits)
Junxiao Shida93f1f2015-11-11 06:13:16 -0700509{
510 traits.setFaceId(face.getId())
511 .setRemoteUri(face.getRemoteUri().toString())
512 .setLocalUri(face.getLocalUri().toString())
513 .setFaceScope(face.getScope())
514 .setFacePersistency(face.getPersistency())
515 .setLinkType(face.getLinkType());
Junxiao Shida93f1f2015-11-11 06:13:16 -0700516}
517
518void
Junxiao Shiae04d342016-07-19 13:20:22 +0000519FaceManager::notifyAddFace(const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700520{
521 ndn::nfd::FaceEventNotification notification;
522 notification.setKind(ndn::nfd::FACE_EVENT_CREATED);
Junxiao Shiae04d342016-07-19 13:20:22 +0000523 collectFaceProperties(face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700524
Junxiao Shiae04d342016-07-19 13:20:22 +0000525 m_postNotification(notification.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700526}
527
528void
Junxiao Shiae04d342016-07-19 13:20:22 +0000529FaceManager::notifyRemoveFace(const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700530{
531 ndn::nfd::FaceEventNotification notification;
532 notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED);
Junxiao Shiae04d342016-07-19 13:20:22 +0000533 collectFaceProperties(face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700534
Junxiao Shiae04d342016-07-19 13:20:22 +0000535 m_postNotification(notification.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700536}
537
538void
539FaceManager::processConfig(const ConfigSection& configSection,
540 bool isDryRun,
541 const std::string& filename)
542{
543 bool hasSeenUnix = false;
544 bool hasSeenTcp = false;
545 bool hasSeenUdp = false;
546 bool hasSeenEther = false;
547 bool hasSeenWebSocket = false;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200548 auto nicList = listNetworkInterfaces();
Yanbiao Li73860e32015-08-19 16:30:16 -0700549
550 for (const auto& item : configSection) {
551 if (item.first == "unix") {
552 if (hasSeenUnix) {
553 BOOST_THROW_EXCEPTION(Error("Duplicate \"unix\" section"));
554 }
555 hasSeenUnix = true;
556
557 processSectionUnix(item.second, isDryRun);
558 }
559 else if (item.first == "tcp") {
560 if (hasSeenTcp) {
561 BOOST_THROW_EXCEPTION(Error("Duplicate \"tcp\" section"));
562 }
563 hasSeenTcp = true;
564
565 processSectionTcp(item.second, isDryRun);
566 }
567 else if (item.first == "udp") {
568 if (hasSeenUdp) {
569 BOOST_THROW_EXCEPTION(Error("Duplicate \"udp\" section"));
570 }
571 hasSeenUdp = true;
572
573 processSectionUdp(item.second, isDryRun, nicList);
574 }
575 else if (item.first == "ether") {
576 if (hasSeenEther) {
577 BOOST_THROW_EXCEPTION(Error("Duplicate \"ether\" section"));
578 }
579 hasSeenEther = true;
580
581 processSectionEther(item.second, isDryRun, nicList);
582 }
583 else if (item.first == "websocket") {
584 if (hasSeenWebSocket) {
585 BOOST_THROW_EXCEPTION(Error("Duplicate \"websocket\" section"));
586 }
587 hasSeenWebSocket = true;
588
589 processSectionWebSocket(item.second, isDryRun);
590 }
591 else {
592 BOOST_THROW_EXCEPTION(Error("Unrecognized option \"" + item.first + "\""));
593 }
594 }
595}
596
597void
598FaceManager::processSectionUnix(const ConfigSection& configSection, bool isDryRun)
599{
600 // ; the unix section contains settings of Unix stream faces and channels
601 // unix
602 // {
603 // path /var/run/nfd.sock ; Unix stream listener path
604 // }
605
606#if defined(HAVE_UNIX_SOCKETS)
Yanbiao Li73860e32015-08-19 16:30:16 -0700607 std::string path = "/var/run/nfd.sock";
608
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200609 for (const auto& i : configSection) {
610 if (i.first == "path") {
611 path = i.second.get_value<std::string>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700612 }
613 else {
614 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200615 i.first + "\" in \"unix\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700616 }
617 }
618
619 if (!isDryRun) {
620 if (m_factories.count("unix") > 0) {
621 return;
622 }
623
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200624 auto factory = make_shared<UnixStreamFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700625 m_factories.insert(std::make_pair("unix", factory));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200626
627 auto channel = factory->createChannel(path);
628 channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700629 }
630#else
631 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, "
632 "cannot process \"unix\" section"));
633#endif // HAVE_UNIX_SOCKETS
634}
635
636void
637FaceManager::processSectionTcp(const ConfigSection& configSection, bool isDryRun)
638{
639 // ; the tcp section contains settings of TCP faces and channels
640 // tcp
641 // {
642 // listen yes ; set to 'no' to disable TCP listener, default 'yes'
643 // port 6363 ; TCP listener port number
644 // }
645
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200646 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700647 bool needToListen = true;
648 bool enableV4 = true;
649 bool enableV6 = true;
650
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200651 for (const auto& i : configSection) {
652 if (i.first == "port") {
653 port = ConfigFile::parseNumber<uint16_t>(i, "tcp");
654 NFD_LOG_TRACE("TCP port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700655 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200656 else if (i.first == "listen") {
657 needToListen = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700658 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200659 else if (i.first == "enable_v4") {
660 enableV4 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700661 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200662 else if (i.first == "enable_v6") {
663 enableV6 = ConfigFile::parseYesNo(i, "tcp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700664 }
665 else {
666 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200667 i.first + "\" in \"tcp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700668 }
669 }
670
671 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200672 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 TCP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700673 " Remove \"tcp\" section to disable TCP channels or"
674 " re-enable at least one channel type."));
675 }
676
677 if (!isDryRun) {
678 if (m_factories.count("tcp") > 0) {
679 return;
680 }
681
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200682 auto factory = make_shared<TcpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700683 m_factories.insert(std::make_pair("tcp", factory));
684
685 if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200686 tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port);
687 shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700688 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200689 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700690 }
691
692 m_factories.insert(std::make_pair("tcp4", factory));
693 }
694
695 if (enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200696 tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port);
697 shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700698 if (needToListen) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200699 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700700 }
701
702 m_factories.insert(std::make_pair("tcp6", factory));
703 }
704 }
705}
706
707void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200708FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700709 const std::vector<NetworkInterfaceInfo>& nicList)
710{
711 // ; the udp section contains settings of UDP faces and channels
712 // udp
713 // {
714 // port 6363 ; UDP unicast port number
715 // idle_timeout 600 ; idle time (seconds) before closing a UDP unicast face
716 // keep_alive_interval 25 ; interval (seconds) between keep-alive refreshes
717
718 // ; NFD creates one UDP multicast face per NIC
719 // mcast yes ; set to 'no' to disable UDP multicast, default 'yes'
720 // mcast_port 56363 ; UDP multicast port number
721 // mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only)
722 // }
723
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200724 uint16_t port = 6363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700725 bool enableV4 = true;
726 bool enableV6 = true;
727 size_t timeout = 600;
728 size_t keepAliveInterval = 25;
729 bool useMcast = true;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200730 auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170");
731 uint16_t mcastPort = 56363;
Yanbiao Li73860e32015-08-19 16:30:16 -0700732
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200733 for (const auto& i : configSection) {
734 if (i.first == "port") {
735 port = ConfigFile::parseNumber<uint16_t>(i, "udp");
736 NFD_LOG_TRACE("UDP unicast port set to " << port);
737 }
738 else if (i.first == "enable_v4") {
739 enableV4 = ConfigFile::parseYesNo(i, "udp");
740 }
741 else if (i.first == "enable_v6") {
742 enableV6 = ConfigFile::parseYesNo(i, "udp");
743 }
744 else if (i.first == "idle_timeout") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700745 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200746 timeout = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700747 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200748 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700749 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200750 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700751 }
752 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200753 else if (i.first == "keep_alive_interval") {
Yanbiao Li73860e32015-08-19 16:30:16 -0700754 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200755 keepAliveInterval = i.second.get_value<size_t>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700756 /// \todo Make use of keepAliveInterval
Yanbiao Li73860e32015-08-19 16:30:16 -0700757 (void)(keepAliveInterval);
758 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200759 catch (const boost::property_tree::ptree_bad_data&) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700760 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200761 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700762 }
763 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200764 else if (i.first == "mcast") {
765 useMcast = ConfigFile::parseYesNo(i, "udp");
Yanbiao Li73860e32015-08-19 16:30:16 -0700766 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200767 else if (i.first == "mcast_port") {
768 mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp");
769 NFD_LOG_TRACE("UDP multicast port set to " << mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700770 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200771 else if (i.first == "mcast_group") {
772 boost::system::error_code ec;
773 mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec);
774 if (ec) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700775 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200776 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700777 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200778 NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700779 }
780 else {
781 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200782 i.first + "\" in \"udp\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700783 }
784 }
785
786 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200787 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 UDP channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700788 " Remove \"udp\" section to disable UDP channels or"
789 " re-enable at least one channel type."));
790 }
791 else if (useMcast && !enableV4) {
792 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 multicast requested, but IPv4 channels"
793 " have been disabled (conflicting configuration options set)"));
794 }
795
796 if (!isDryRun) {
797 shared_ptr<UdpFactory> factory;
798 bool isReload = false;
799 if (m_factories.count("udp") > 0) {
800 isReload = true;
801 factory = static_pointer_cast<UdpFactory>(m_factories["udp"]);
802 }
803 else {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200804 factory = make_shared<UdpFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700805 m_factories.insert(std::make_pair("udp", factory));
806 }
807
808 if (!isReload && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200809 udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port);
810 shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout));
811 v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700812
813 m_factories.insert(std::make_pair("udp4", factory));
814 }
815
816 if (!isReload && enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200817 udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port);
818 shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout));
819 v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700820
Yanbiao Li73860e32015-08-19 16:30:16 -0700821 m_factories.insert(std::make_pair("udp6", factory));
822 }
823
Junxiao Shicde37ad2015-12-24 01:02:05 -0700824 std::set<shared_ptr<Face>> multicastFacesToRemove;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200825 for (const auto& i : factory->getMulticastFaces()) {
826 multicastFacesToRemove.insert(i.second);
827 }
828
Yanbiao Li73860e32015-08-19 16:30:16 -0700829 if (useMcast && enableV4) {
830 std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces;
831 for (const auto& nic : nicList) {
832 if (nic.isUp() && nic.isMulticastCapable() && !nic.ipv4Addresses.empty()) {
833 ipv4MulticastInterfaces.push_back(nic);
834 }
835 }
836
837 bool isNicNameNecessary = false;
838#if defined(__linux__)
839 if (ipv4MulticastInterfaces.size() > 1) {
840 // On Linux if we have more than one MulticastUdpFace
841 // we need to specify the name of the interface
842 isNicNameNecessary = true;
843 }
844#endif
845
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200846 udp::Endpoint mcastEndpoint(mcastGroup, mcastPort);
Yanbiao Li73860e32015-08-19 16:30:16 -0700847 for (const auto& nic : ipv4MulticastInterfaces) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200848 udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort);
849 auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint,
Yukai Tu0a49d342015-09-13 12:54:22 +0800850 isNicNameNecessary ? nic.name : "");
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200851 m_faceTable.add(newFace);
852 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700853 }
854 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700855
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200856 for (const auto& face : multicastFacesToRemove) {
857 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700858 }
859 }
860}
861
862void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200863FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun,
Yanbiao Li73860e32015-08-19 16:30:16 -0700864 const std::vector<NetworkInterfaceInfo>& nicList)
865{
866 // ; the ether section contains settings of Ethernet faces and channels
867 // ether
868 // {
869 // ; NFD creates one Ethernet multicast face per NIC
870 // mcast yes ; set to 'no' to disable Ethernet multicast, default 'yes'
871 // mcast_group 01:00:5E:00:17:AA ; Ethernet multicast group
872 // }
873
874#if defined(HAVE_LIBPCAP)
875 bool useMcast = true;
876 ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress());
877
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200878 for (const auto& i : configSection) {
879 if (i.first == "mcast") {
880 useMcast = ConfigFile::parseYesNo(i, "ether");
Yanbiao Li73860e32015-08-19 16:30:16 -0700881 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200882 else if (i.first == "mcast_group") {
883 mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>());
Yanbiao Li73860e32015-08-19 16:30:16 -0700884 if (mcastGroup.isNull()) {
885 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200886 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700887 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200888 NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup);
Yanbiao Li73860e32015-08-19 16:30:16 -0700889 }
890 else {
891 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200892 i.first + "\" in \"ether\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700893 }
894 }
895
896 if (!isDryRun) {
897 shared_ptr<EthernetFactory> factory;
898 if (m_factories.count("ether") > 0) {
899 factory = static_pointer_cast<EthernetFactory>(m_factories["ether"]);
900 }
901 else {
902 factory = make_shared<EthernetFactory>();
903 m_factories.insert(std::make_pair("ether", factory));
904 }
905
Junxiao Shicde37ad2015-12-24 01:02:05 -0700906 std::set<shared_ptr<Face>> multicastFacesToRemove;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200907 for (const auto& i : factory->getMulticastFaces()) {
908 multicastFacesToRemove.insert(i.second);
909 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700910
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200911 if (useMcast) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700912 for (const auto& nic : nicList) {
913 if (nic.isUp() && nic.isMulticastCapable()) {
914 try {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200915 auto newFace = factory->createMulticastFace(nic, mcastGroup);
916 m_faceTable.add(newFace);
917 multicastFacesToRemove.erase(newFace);
Yanbiao Li73860e32015-08-19 16:30:16 -0700918 }
919 catch (const EthernetFactory::Error& factoryError) {
920 NFD_LOG_ERROR(factoryError.what() << ", continuing");
921 }
Davide Pesavento35120ea2015-11-17 21:13:18 +0100922 catch (const face::EthernetTransport::Error& faceError) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700923 NFD_LOG_ERROR(faceError.what() << ", continuing");
924 }
925 }
926 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700927 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700928
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200929 for (const auto& face : multicastFacesToRemove) {
930 face->close();
Yanbiao Li73860e32015-08-19 16:30:16 -0700931 }
932 }
933#else
934 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without libpcap, cannot process \"ether\" section"));
935#endif // HAVE_LIBPCAP
936}
937
938void
939FaceManager::processSectionWebSocket(const ConfigSection& configSection, bool isDryRun)
940{
941 // ; the websocket section contains settings of WebSocket faces and channels
942 // websocket
943 // {
944 // listen yes ; set to 'no' to disable WebSocket listener, default 'yes'
945 // port 9696 ; WebSocket listener port number
946 // enable_v4 yes ; set to 'no' to disable listening on IPv4 socket, default 'yes'
947 // enable_v6 yes ; set to 'no' to disable listening on IPv6 socket, default 'yes'
948 // }
949
950#if defined(HAVE_WEBSOCKET)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200951 uint16_t port = 9696;
Yanbiao Li73860e32015-08-19 16:30:16 -0700952 bool needToListen = true;
953 bool enableV4 = true;
954 bool enableV6 = true;
955
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200956 for (const auto& i : configSection) {
957 if (i.first == "port") {
958 port = ConfigFile::parseNumber<uint16_t>(i, "websocket");
959 NFD_LOG_TRACE("WebSocket port set to " << port);
Yanbiao Li73860e32015-08-19 16:30:16 -0700960 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200961 else if (i.first == "listen") {
962 needToListen = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700963 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200964 else if (i.first == "enable_v4") {
965 enableV4 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700966 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200967 else if (i.first == "enable_v6") {
968 enableV6 = ConfigFile::parseYesNo(i, "websocket");
Yanbiao Li73860e32015-08-19 16:30:16 -0700969 }
970 else {
971 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200972 i.first + "\" in \"websocket\" section"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700973 }
974 }
975
976 if (!enableV4 && !enableV6) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200977 BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 WebSocket channels have been disabled."
Yanbiao Li73860e32015-08-19 16:30:16 -0700978 " Remove \"websocket\" section to disable WebSocket channels or"
979 " re-enable at least one channel type."));
980 }
981
982 if (!enableV4 && enableV6) {
983 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel."));
984 }
985
986 if (!isDryRun) {
987 if (m_factories.count("websocket") > 0) {
988 return;
989 }
990
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200991 auto factory = make_shared<WebSocketFactory>();
Yanbiao Li73860e32015-08-19 16:30:16 -0700992 m_factories.insert(std::make_pair("websocket", factory));
993
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200994 shared_ptr<WebSocketChannel> channel;
995
Yanbiao Li73860e32015-08-19 16:30:16 -0700996 if (enableV6 && enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200997 websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port);
998 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -0700999
1000 m_factories.insert(std::make_pair("websocket46", factory));
1001 }
1002 else if (enableV4) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +02001003 websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port);
1004 channel = factory->createChannel(endpoint);
Yanbiao Li73860e32015-08-19 16:30:16 -07001005
1006 m_factories.insert(std::make_pair("websocket4", factory));
1007 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +02001008
1009 if (channel && needToListen) {
1010 channel->listen(bind(&FaceTable::add, &m_faceTable, _1));
1011 }
Yanbiao Li73860e32015-08-19 16:30:16 -07001012 }
1013#else
1014 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, "
1015 "cannot process \"websocket\" section"));
1016#endif // HAVE_WEBSOCKET
1017}
1018
Davide Pesavento1d7e7af2015-10-10 23:54:08 +02001019} // namespace nfd