31 #include <boost/range/adaptor/map.hpp> 32 #include <boost/range/algorithm/copy.hpp> 37 namespace ip = boost::asio::ip;
38 namespace net = ndn::net;
46 static std::string id(
"udp");
53 m_netifAddConn =
netmon->onInterfaceAdded.connect([
this] (
const auto& netif) {
54 this->applyMcastConfigToNetif(netif);
86 bool wantListen =
true;
88 bool enableV4 =
false;
89 bool enableV6 =
false;
90 uint32_t idleTimeout = 600;
91 MulticastConfig mcastConfig;
95 enableV4 = enableV6 = mcastConfig.isEnabled =
true;
97 for (
const auto& pair : *configSection) {
98 const std::string& key = pair.first;
101 if (key ==
"listen") {
104 else if (key ==
"port") {
105 port = ConfigFile::parseNumber<uint16_t>(pair,
"face_system.udp");
107 else if (key ==
"enable_v4") {
110 else if (key ==
"enable_v6") {
113 else if (key ==
"idle_timeout") {
114 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair,
"face_system.udp");
116 else if (key ==
"keep_alive_interval") {
119 else if (key ==
"mcast") {
122 else if (key ==
"mcast_group") {
123 const std::string& valueStr = value.get_value<std::string>();
124 boost::system::error_code ec;
125 mcastConfig.group.address(ip::address_v4::from_string(valueStr, ec));
128 valueStr +
"' cannot be parsed as an IPv4 address"));
130 else if (!mcastConfig.group.address().is_multicast()) {
132 valueStr +
"' is not a multicast address"));
135 else if (key ==
"mcast_port") {
136 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair,
"face_system.udp"));
138 else if (key ==
"mcast_group_v6") {
139 const std::string& valueStr = value.get_value<std::string>();
140 boost::system::error_code ec;
141 mcastConfig.groupV6.address(ip::address_v6::from_string(valueStr, ec));
144 valueStr +
"' cannot be parsed as an IPv6 address"));
146 else if (!mcastConfig.groupV6.address().is_multicast()) {
148 valueStr +
"' is not a multicast address"));
151 else if (key ==
"mcast_port_v6") {
152 mcastConfig.groupV6.port(ConfigFile::parseNumber<uint16_t>(pair,
"face_system.udp"));
154 else if (key ==
"mcast_ad_hoc") {
156 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
158 else if (key ==
"whitelist") {
159 mcastConfig.netifPredicate.parseWhitelist(value);
161 else if (key ==
"blacklist") {
162 mcastConfig.netifPredicate.parseBlacklist(value);
165 BOOST_THROW_EXCEPTION(
ConfigFile::Error(
"Unrecognized option face_system.udp." + key));
169 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
171 "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. " 172 "Remove face_system.udp section to disable UDP channels or enable at least one of them."));
182 shared_ptr<UdpChannel> v4Channel = this->
createChannel(endpoint, time::seconds(idleTimeout));
183 if (wantListen && !v4Channel->isListening()) {
184 v4Channel->listen(this->
addFace,
nullptr);
190 NFD_LOG_WARN(
"Cannot close udp4 channel after its creation");
195 shared_ptr<UdpChannel> v6Channel = this->
createChannel(endpoint, time::seconds(idleTimeout));
196 if (wantListen && !v6Channel->isListening()) {
197 v6Channel->listen(this->
addFace,
nullptr);
203 NFD_LOG_WARN(
"Cannot close udp6 channel after its creation");
206 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
207 if (mcastConfig.isEnabled) {
208 NFD_LOG_INFO(
"enabling multicast on " << mcastConfig.group);
209 NFD_LOG_INFO(
"enabling multicast on " << mcastConfig.groupV6);
215 else if (mcastConfig.isEnabled) {
216 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
217 NFD_LOG_WARN(
"Cannot change ad hoc setting on existing faces");
219 if (m_mcastConfig.group != mcastConfig.group) {
220 NFD_LOG_INFO(
"changing IPv4 multicast group from " << m_mcastConfig.group <<
221 " to " << mcastConfig.group);
223 if (m_mcastConfig.groupV6 != mcastConfig.groupV6) {
224 NFD_LOG_INFO(
"changing IPv6 multicast group from " << m_mcastConfig.groupV6 <<
225 " to " << mcastConfig.groupV6);
227 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
234 m_mcastConfig = mcastConfig;
235 this->applyMcastConfig(context);
243 BOOST_ASSERT(req.
remoteUri.isCanonical());
246 NFD_LOG_TRACE(
"Cannot create unicast UDP face with LocalUri");
247 onFailure(406,
"Unicast UDP faces cannot be created with a LocalUri");
252 NFD_LOG_TRACE(
"createFace does not support FACE_PERSISTENCY_ON_DEMAND");
253 onFailure(406,
"Outgoing UDP faces do not support on-demand persistency");
258 boost::lexical_cast<uint16_t>(req.
remoteUri.getPort()));
260 if (endpoint.address().is_multicast()) {
261 NFD_LOG_TRACE(
"createFace does not support multicast faces");
262 onFailure(406,
"Cannot create multicast UDP faces");
268 NFD_LOG_TRACE(
"createFace cannot create non-local face with local fields enabled");
269 onFailure(406,
"Local fields can only be enabled on faces with local scope");
281 for (
const auto& i : m_channels) {
282 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
283 (i.first.address().is_v6() && endpoint.address().is_v6())) {
284 i.second->connect(endpoint, req.
params, onCreated, onFailure);
289 NFD_LOG_TRACE(
"No channels available to connect to " << endpoint);
290 onFailure(504,
"No channels available to connect");
293 shared_ptr<UdpChannel>
295 time::nanoseconds idleTimeout)
297 auto it = m_channels.find(localEndpoint);
298 if (it != m_channels.end())
302 if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) {
303 BOOST_THROW_EXCEPTION(
Error(
"Cannot create UDP channel on " +
304 boost::lexical_cast<std::string>(localEndpoint) +
305 ", endpoint already allocated for a UDP multicast face"));
308 auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout, m_wantCongestionMarking);
309 m_channels[localEndpoint] = channel;
314 std::vector<shared_ptr<const Channel>>
322 const ip::address& localAddress,
325 BOOST_ASSERT(multicastEndpoint.address().is_multicast());
327 udp::Endpoint localEp(localAddress, multicastEndpoint.port());
328 BOOST_ASSERT(localEp.protocol() == multicastEndpoint.protocol());
330 auto mcastEp = multicastEndpoint;
331 if (mcastEp.address().is_v6()) {
333 auto mcastAddress = mcastEp.address().to_v6();
334 mcastAddress.scope_id(netif->getIndex());
335 mcastEp.address(mcastAddress);
339 auto it = m_mcastFaces.find(localEp);
340 if (it != m_mcastFaces.end()) {
341 if (it->second->getRemoteUri() == FaceUri(mcastEp))
344 BOOST_THROW_EXCEPTION(
Error(
"Cannot create UDP multicast face on " +
345 boost::lexical_cast<std::string>(localEp) +
346 ", endpoint already allocated for a different UDP multicast face"));
350 if (m_channels.find(localEp) != m_channels.end()) {
351 BOOST_THROW_EXCEPTION(
Error(
"Cannot create UDP multicast face on " +
352 boost::lexical_cast<std::string>(localEp) +
353 ", endpoint already allocated for a UDP channel"));
363 auto linkService = make_unique<GenericLinkService>(options);
364 auto transport = make_unique<MulticastUdpTransport>(mcastEp, std::move(rxSock), std::move(txSock),
365 m_mcastConfig.linkType);
366 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
368 m_mcastFaces[localEp] = face;
374 static optional<ip::address>
375 pickAddress(
const net::NetworkInterface& netif, net::AddressFamily af)
377 for (
const auto& na : netif.getNetworkAddresses()) {
378 if (na.getFamily() == af &&
379 (na.getScope() == net::AddressScope::LINK || na.getScope() == net::AddressScope::GLOBAL)) {
386 std::vector<shared_ptr<Face>>
387 UdpFactory::applyMcastConfigToNetif(
const shared_ptr<const net::NetworkInterface>& netif)
389 BOOST_ASSERT(netif !=
nullptr);
391 if (!m_mcastConfig.isEnabled) {
395 if (!netif->isUp()) {
396 NFD_LOG_DEBUG(
"Not creating multicast faces on " << netif->getName() <<
": netif is down");
400 if (netif->isLoopback()) {
401 NFD_LOG_DEBUG(
"Not creating multicast faces on " << netif->getName() <<
": netif is loopback");
405 if (!netif->canMulticast()) {
406 NFD_LOG_DEBUG(
"Not creating multicast faces on " << netif->getName() <<
": netif cannot multicast");
410 if (!m_mcastConfig.netifPredicate(*netif)) {
411 NFD_LOG_DEBUG(
"Not creating multicast faces on " << netif->getName() <<
": rejected by whitelist/blacklist");
415 std::vector<ip::address> addrs;
416 for (
auto af : {net::AddressFamily::V4, net::AddressFamily::V6}) {
419 addrs.push_back(*addr);
423 NFD_LOG_DEBUG(
"Not creating multicast faces on " << netif->getName() <<
": no viable IP address");
425 m_netifConns[netif->getIndex()].addrAddConn =
426 netif->onAddressAdded.connect([=] (
auto...) { this->applyMcastConfigToNetif(netif); });
430 NFD_LOG_DEBUG(
"Creating multicast faces on " << netif->getName());
432 std::vector<shared_ptr<Face>> faces;
433 for (
const auto& addr : addrs) {
435 addr.is_v4() ? m_mcastConfig.group : m_mcastConfig.groupV6);
440 faces.push_back(std::move(face));
450 std::set<shared_ptr<Face>> facesToClose;
451 boost::copy(m_mcastFaces | boost::adaptors::map_values,
452 std::inserter(facesToClose, facesToClose.end()));
455 for (
const auto& netif :
netmon->listNetworkInterfaces()) {
456 auto facesToKeep = this->applyMcastConfigToNetif(netif);
457 for (
const auto& face : facesToKeep) {
459 facesToClose.erase(face);
464 for (
const auto& face : facesToClose) {
UdpFactory(const CtorParams ¶ms)
FaceCreatedCallback addFace
callback when a new face is created
optional< FaceUri > localUri
std::set< std::string > providedSchemes
FaceUri schemes provided by this ProtocolFactory.
static optional< ip::address > pickAddress(const net::NetworkInterface &netif, net::AddressFamily af)
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 openTxSocket(protocol::socket &sock, const protocol::endpoint &localEndpoint, const shared_ptr< const ndn::net::NetworkInterface > &netif=nullptr, bool enableLoopback=false)
std::vector< shared_ptr< const Channel > > getChannels() const override
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.
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"
shared_ptr< ndn::net::NetworkMonitor > netmon
NetworkMonitor for listing available network interfaces and monitoring their changes.
shared_ptr< UdpChannel > createChannel(const udp::Endpoint &localEndpoint, time::nanoseconds idleTimeout)
Create UDP-based channel using udp::Endpoint.
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...
#define NFD_REGISTER_PROTOCOL_FACTORY(PF)
registers a protocol factory
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
context for processing a config section in ProtocolFactory
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
boost::property_tree::ptree ConfigSection
a config file section
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
GeneralConfig generalConfig
ndn::nfd::FacePersistency persistency
boost::asio::ip::udp::endpoint Endpoint
Provides support for an underlying protocol.
Options that control the behavior of GenericLinkService.
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context) override
process face_system.udp config section
Parameters to ProtocolFactory constructor.
void createFace(const CreateFaceRequest &req, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure) override
Try to create a unicast face using the supplied parameters.
static const std::string & getId()
#define NFD_LOG_INIT(name)
bool wantCongestionMarking
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)
static constexpr ssize_t MIN_MTU
minimum MTU that may be set on a transport
bool allowCongestionMarking
enables send queue congestion detection and marking
const FaceId INVALID_FACEID
indicates an invalid FaceId
Encapsulates a face creation request and all its parameters.
boost::asio::io_service & getGlobalIoService()