31 #include <ndn-cxx/net/address-converter.hpp> 32 #include <boost/range/adaptor/map.hpp> 33 #include <boost/range/algorithm/copy.hpp> 38 namespace ip = boost::asio::ip;
47 static std::string id(
"udp");
54 m_netifAddConn =
netmon->onInterfaceAdded.connect(bind(&UdpFactory::applyMcastConfigToNetif,
this, _1));
85 bool wantListen =
true;
87 bool enableV4 =
false;
88 bool enableV6 =
false;
89 uint32_t idleTimeout = 600;
90 MulticastConfig mcastConfig;
94 enableV4 = enableV6 = mcastConfig.isEnabled =
true;
96 for (
const auto& pair : *configSection) {
97 const std::string& key = pair.first;
100 if (key ==
"listen") {
103 else if (key ==
"port") {
104 port = ConfigFile::parseNumber<uint16_t>(pair,
"face_system.udp");
106 else if (key ==
"enable_v4") {
109 else if (key ==
"enable_v6") {
112 else if (key ==
"idle_timeout") {
113 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair,
"face_system.udp");
115 else if (key ==
"keep_alive_interval") {
118 else if (key ==
"mcast") {
121 else if (key ==
"mcast_group") {
122 const std::string& valueStr = value.get_value<std::string>();
123 boost::system::error_code ec;
124 mcastConfig.group.address(ip::address_v4::from_string(valueStr, ec));
127 valueStr +
"' cannot be parsed as an IPv4 address"));
129 else if (!mcastConfig.group.address().is_multicast()) {
131 valueStr +
"' is not a multicast address"));
134 else if (key ==
"mcast_port") {
135 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair,
"face_system.udp"));
137 else if (key ==
"mcast_group_v6") {
138 const std::string& valueStr = value.get_value<std::string>();
139 boost::system::error_code ec;
143 valueStr +
"' cannot be parsed as an IPv6 address"));
145 else if (!mcastConfig.groupV6.address().is_multicast()) {
147 valueStr +
"' is not a multicast address"));
150 else if (key ==
"mcast_port_v6") {
151 mcastConfig.groupV6.port(ConfigFile::parseNumber<uint16_t>(pair,
"face_system.udp"));
153 else if (key ==
"mcast_ad_hoc") {
157 else if (key ==
"whitelist") {
158 mcastConfig.netifPredicate.parseWhitelist(value);
160 else if (key ==
"blacklist") {
161 mcastConfig.netifPredicate.parseBlacklist(value);
164 BOOST_THROW_EXCEPTION(
ConfigFile::Error(
"Unrecognized option face_system.udp." + key));
168 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
170 "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. " 171 "Remove face_system.udp section to disable UDP channels or enable at least one of them."));
181 shared_ptr<UdpChannel> v4Channel = this->
createChannel(endpoint, time::seconds(idleTimeout));
182 if (wantListen && !v4Channel->isListening()) {
183 v4Channel->listen(this->
addFace,
nullptr);
189 NFD_LOG_WARN(
"Cannot close udp4 channel after its creation");
194 shared_ptr<UdpChannel> v6Channel = this->
createChannel(endpoint, time::seconds(idleTimeout));
195 if (wantListen && !v6Channel->isListening()) {
196 v6Channel->listen(this->
addFace,
nullptr);
202 NFD_LOG_WARN(
"Cannot close udp6 channel after its creation");
205 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
206 if (mcastConfig.isEnabled) {
207 NFD_LOG_INFO(
"enabling multicast on " << mcastConfig.group);
208 NFD_LOG_INFO(
"enabling multicast on " << mcastConfig.groupV6);
214 else if (mcastConfig.isEnabled) {
215 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
216 NFD_LOG_WARN(
"Cannot change ad hoc setting on existing faces");
218 if (m_mcastConfig.group != mcastConfig.group) {
219 NFD_LOG_INFO(
"changing IPv4 multicast group from " << m_mcastConfig.group <<
220 " to " << mcastConfig.group);
222 if (m_mcastConfig.groupV6 != mcastConfig.groupV6) {
223 NFD_LOG_INFO(
"changing IPv6 multicast group from " << m_mcastConfig.groupV6 <<
224 " to " << mcastConfig.groupV6);
226 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
233 m_mcastConfig = mcastConfig;
234 this->applyMcastConfig(context);
245 NFD_LOG_TRACE(
"Cannot create unicast UDP face with LocalUri");
246 onFailure(406,
"Unicast UDP faces cannot be created with a LocalUri");
251 NFD_LOG_TRACE(
"createFace does not support FACE_PERSISTENCY_ON_DEMAND");
252 onFailure(406,
"Outgoing UDP faces do not support on-demand persistency");
259 if (endpoint.address().is_multicast()) {
260 NFD_LOG_TRACE(
"createFace does not support multicast faces");
261 onFailure(406,
"Cannot create multicast UDP faces");
267 NFD_LOG_TRACE(
"createFace cannot create non-local face with local fields enabled");
268 onFailure(406,
"Local fields can only be enabled on faces with local scope");
273 for (
const auto& i : m_channels) {
274 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
275 (i.first.address().is_v6() && endpoint.address().is_v6())) {
276 i.second->connect(endpoint, req.
params, onCreated, onFailure);
281 NFD_LOG_TRACE(
"No channels available to connect to " << endpoint);
282 onFailure(504,
"No channels available to connect");
285 shared_ptr<UdpChannel>
287 time::nanoseconds idleTimeout)
289 auto it = m_channels.find(localEndpoint);
290 if (it != m_channels.end())
294 if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) {
295 BOOST_THROW_EXCEPTION(
Error(
"Cannot create UDP channel on " +
296 boost::lexical_cast<std::string>(localEndpoint) +
297 ", endpoint already allocated for a UDP multicast face"));
300 auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout, m_wantCongestionMarking);
301 m_channels[localEndpoint] = channel;
306 std::vector<shared_ptr<const Channel>>
314 const ip::address& localAddress,
317 BOOST_ASSERT(multicastEndpoint.address().is_multicast());
319 udp::Endpoint localEp(localAddress, multicastEndpoint.port());
320 BOOST_ASSERT(localEp.protocol() == multicastEndpoint.protocol());
322 auto mcastEp = multicastEndpoint;
323 if (mcastEp.address().is_v6()) {
325 auto mcastAddress = mcastEp.address().to_v6();
326 mcastAddress.scope_id(netif->getIndex());
327 mcastEp.address(mcastAddress);
331 auto it = m_mcastFaces.find(localEp);
332 if (it != m_mcastFaces.end()) {
333 if (it->second->getRemoteUri() ==
FaceUri(mcastEp))
336 BOOST_THROW_EXCEPTION(
Error(
"Cannot create UDP multicast face on " +
337 boost::lexical_cast<std::string>(localEp) +
338 ", endpoint already allocated for a different UDP multicast face"));
342 if (m_channels.find(localEp) != m_channels.end()) {
343 BOOST_THROW_EXCEPTION(
Error(
"Cannot create UDP multicast face on " +
344 boost::lexical_cast<std::string>(localEp) +
345 ", endpoint already allocated for a UDP channel"));
355 auto linkService = make_unique<GenericLinkService>(options);
356 auto transport = make_unique<MulticastUdpTransport>(mcastEp, std::move(rxSock), std::move(txSock),
357 m_mcastConfig.linkType);
358 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
360 m_mcastFaces[localEp] =
face;
370 if (na.getFamily() == af &&
371 (na.getScope() == net::AddressScope::LINK || na.getScope() == net::AddressScope::GLOBAL)) {
378 std::vector<shared_ptr<Face>>
379 UdpFactory::applyMcastConfigToNetif(
const shared_ptr<const net::NetworkInterface>& netif)
381 BOOST_ASSERT(netif !=
nullptr);
383 if (!m_mcastConfig.isEnabled) {
387 if (!netif->isUp()) {
388 NFD_LOG_DEBUG(
"Not creating multicast faces on " << netif->getName() <<
": netif is down");
392 if (netif->isLoopback()) {
393 NFD_LOG_DEBUG(
"Not creating multicast faces on " << netif->getName() <<
": netif is loopback");
397 if (!netif->canMulticast()) {
398 NFD_LOG_DEBUG(
"Not creating multicast faces on " << netif->getName() <<
": netif cannot multicast");
402 if (!m_mcastConfig.netifPredicate(*netif)) {
403 NFD_LOG_DEBUG(
"Not creating multicast faces on " << netif->getName() <<
": rejected by whitelist/blacklist");
407 std::vector<ip::address> addrs;
408 for (
auto af : {net::AddressFamily::V4, net::AddressFamily::V6}) {
411 addrs.push_back(*addr);
415 NFD_LOG_DEBUG(
"Not creating multicast faces on " << netif->getName() <<
": no viable IP address");
417 m_netifConns[netif->getIndex()].addrAddConn =
418 netif->onAddressAdded.connect(bind(&UdpFactory::applyMcastConfigToNetif,
this, netif));
422 NFD_LOG_DEBUG(
"Creating multicast faces on " << netif->getName());
424 std::vector<shared_ptr<Face>> faces;
425 for (
const auto& addr : addrs) {
427 addr.is_v4() ? m_mcastConfig.group : m_mcastConfig.groupV6);
432 faces.push_back(std::move(
face));
439 UdpFactory::applyMcastConfig(
const FaceSystem::ConfigContext& context)
442 std::set<shared_ptr<Face>> facesToClose;
443 boost::copy(m_mcastFaces | boost::adaptors::map_values,
444 std::inserter(facesToClose, facesToClose.end()));
447 for (
const auto& netif :
netmon->listNetworkInterfaces()) {
448 auto facesToKeep = this->applyMcastConfigToNetif(netif);
449 for (
const auto&
face : facesToKeep) {
451 facesToClose.erase(
face);
456 for (
const auto&
face : facesToClose) {
std::set< std::string > providedSchemes
FaceUri schemes provided by this ProtocolFactory.
void createFace(const CreateFaceRequest &req, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure) override
Try to create a unicast face using the supplied parameters.
constexpr nullopt_t nullopt
shared_ptr< Face > createMulticastFace(const shared_ptr< const ndn::net::NetworkInterface > &netif, const boost::asio::ip::address &localAddress, const udp::Endpoint &multicastEndpoint)
Create a multicast UDP face.
static void openRxSocket(protocol::socket &sock, const protocol::endpoint &multicastGroup, const boost::asio::ip::address &localAddress, const shared_ptr< const ndn::net::NetworkInterface > &netif=nullptr)
bool wantCongestionMarking
const std::string & getHost() const
get host (domain)
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string §ionName)
parse a config option that can be either "yes" or "no"
std::vector< shared_ptr< const Channel > > getChannels() const override
shared_ptr< ndn::net::NetworkMonitor > netmon
NetworkMonitor for listing available network interfaces and monitoring their changes.
GeneralConfig generalConfig
static optional< ip::address > pickAddress(const net::NetworkInterface &netif, net::AddressFamily af)
const std::string & getPort() const
get port
detail::SimulatorIo & getGlobalIoService()
#define NFD_LOG_DEBUG(expression)
NFD_REGISTER_PROTOCOL_FACTORY(EthernetFactory)
std::function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
std::function< void(const shared_ptr< Face > &face)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
static const std::string & getId()
#define NFD_LOG_INFO(expression)
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
protocol factory for UDP over IPv4 and IPv6
FaceCreatedCallback addFace
callback when a new face is created
Represents one network interface attached to the host.
#define NFD_LOG_TRACE(expression)
Provides support for an underlying protocol.
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context) override
process face_system.udp config section
bool allowCongestionMarking
enables send queue congestion detection and marking
Copyright (c) 2011-2015 Regents of the University of California.
static void openTxSocket(protocol::socket &sock, const protocol::endpoint &localEndpoint, const shared_ptr< const ndn::net::NetworkInterface > &netif=nullptr, bool enableLoopback=false)
shared_ptr< UdpChannel > createChannel(const udp::Endpoint &localEndpoint, time::nanoseconds idleTimeout)
Create UDP-based channel using udp::Endpoint.
Parameters to ProtocolFactory constructor.
boost::property_tree::ptree ConfigSection
a config file section
ndn::nfd::FacePersistency persistency
represents the underlying protocol and address used by a Face
context for processing a config section in ProtocolFactory
boost::asio::ip::udp::endpoint Endpoint
boost::asio::ip::address addressFromString(const std::string &address, boost::system::error_code &ec)
parse and convert the input string into an IP address
Options that control the behavior of GenericLinkService.
#define NFD_LOG_WARN(expression)
boost::asio::ip::address_v6 addressV6FromString(const std::string &address, boost::system::error_code &ec)
parse and convert the input string into an IPv6 address
Encapsulates a face creation request and all its parameters.
optional< FaceUri > localUri
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
bool isCanonical() const
determine whether this FaceUri is in canonical form
#define NFD_LOG_INIT(name)
const std::set< NetworkAddress > & getNetworkAddresses() const
Returns a list of all network-layer addresses present on the interface.
const FaceId INVALID_FACEID
indicates an invalid FaceId
UdpFactory(const CtorParams ¶ms)