blob: 7b1f4f0959d63cb1fdb20a0524a24442fe5da5e4 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2d491752017-07-14 21:32:05 +00002/*
Junxiao Shi64d99f22017-01-21 23:06:36 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Steve DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Giulio Grassi624f6c62014-02-18 19:42:14 +010025
26#include "udp-factory.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080027#include "generic-link-service.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080028#include "multicast-udp-transport.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010029#include "core/global-io.hpp"
Yanbiao Liafb20592017-08-03 16:20:46 -070030
31#include <ndn-cxx/net/address-converter.hpp>
Davide Pesaventobb734df2017-10-24 18:05:36 -040032#include <boost/range/adaptor/map.hpp>
Junxiao Shi64d99f22017-01-21 23:06:36 +000033#include <boost/range/algorithm/copy.hpp>
Giulio Grassi624f6c62014-02-18 19:42:14 +010034
Davide Pesavento292e5e12015-03-13 02:08:33 +010035#ifdef __linux__
36#include <cerrno> // for errno
37#include <cstring> // for std::strerror()
38#include <sys/socket.h> // for setsockopt()
Junxiao Shi64d99f22017-01-21 23:06:36 +000039#endif // __linux__
Giulio Grassi6d7176d2014-04-16 16:08:48 +020040
Giulio Grassi624f6c62014-02-18 19:42:14 +010041namespace nfd {
Junxiao Shi64d99f22017-01-21 23:06:36 +000042namespace face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010043
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020044namespace ip = boost::asio::ip;
Davide Pesaventobb734df2017-10-24 18:05:36 -040045namespace net = ndn::net;
Junxiao Shi79494162014-04-02 18:25:11 -070046
Giulio Grassi624f6c62014-02-18 19:42:14 +010047NFD_LOG_INIT("UdpFactory");
Junxiao Shib47247d2017-01-24 15:09:16 +000048NFD_REGISTER_PROTOCOL_FACTORY(UdpFactory);
49
50const std::string&
51UdpFactory::getId()
52{
53 static std::string id("udp");
54 return id;
55}
56
Junxiao Shi0ba6d642017-07-17 00:53:22 +000057UdpFactory::UdpFactory(const CtorParams& params)
58 : ProtocolFactory(params)
59{
Junxiao Shibbace1d2017-08-06 20:03:37 +000060 m_netifAddConn = netmon->onInterfaceAdded.connect(bind(&UdpFactory::applyMcastConfigToNetif, this, _1));
Junxiao Shi0ba6d642017-07-17 00:53:22 +000061}
Giulio Grassi624f6c62014-02-18 19:42:14 +010062
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060063void
Junxiao Shi64d99f22017-01-21 23:06:36 +000064UdpFactory::processConfig(OptionalConfigSection configSection,
65 FaceSystem::ConfigContext& context)
66{
67 // udp
68 // {
69 // port 6363
70 // enable_v4 yes
71 // enable_v6 yes
72 // idle_timeout 600
73 // keep_alive_interval 25 ; acceptable but ignored
74 // mcast yes
75 // mcast_group 224.0.23.170
76 // mcast_port 56363
Teng Liangfe4fce32017-03-29 04:49:38 +000077 // mcast_ad_hoc no
Junxiao Shic31080d2017-01-24 15:10:12 +000078 // whitelist
79 // {
80 // *
81 // }
82 // blacklist
83 // {
84 // }
Junxiao Shi64d99f22017-01-21 23:06:36 +000085 // }
86
87 uint16_t port = 6363;
88 bool enableV4 = false;
89 bool enableV6 = false;
90 uint32_t idleTimeout = 600;
91 MulticastConfig mcastConfig;
92
93 if (configSection) {
94 // These default to 'yes' but only if face_system.udp section is present
95 enableV4 = enableV6 = mcastConfig.isEnabled = true;
96
97 for (const auto& pair : *configSection) {
98 const std::string& key = pair.first;
99 const ConfigSection& value = pair.second;
100
101 if (key == "port") {
102 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp");
103 }
104 else if (key == "enable_v4") {
105 enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp");
106 }
107 else if (key == "enable_v6") {
108 enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp");
109 }
110 else if (key == "idle_timeout") {
111 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp");
112 }
113 else if (key == "keep_alive_interval") {
114 // ignored
115 }
116 else if (key == "mcast") {
117 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp");
118 }
119 else if (key == "mcast_group") {
120 const std::string& valueStr = value.get_value<std::string>();
121 boost::system::error_code ec;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400122 mcastConfig.group.address(ip::address_v4::from_string(valueStr, ec));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000123 if (ec) {
124 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
125 valueStr + "' cannot be parsed as an IPv4 address"));
126 }
127 else if (!mcastConfig.group.address().is_multicast()) {
128 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
129 valueStr + "' is not a multicast address"));
130 }
131 }
132 else if (key == "mcast_port") {
133 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
134 }
Teng Liangfe4fce32017-03-29 04:49:38 +0000135 else if (key == "mcast_ad_hoc") {
136 bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.udp");
137 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
138 }
Junxiao Shic31080d2017-01-24 15:10:12 +0000139 else if (key == "whitelist") {
140 mcastConfig.netifPredicate.parseWhitelist(value);
141 }
142 else if (key == "blacklist") {
143 mcastConfig.netifPredicate.parseBlacklist(value);
144 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000145 else {
146 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.udp." + key));
147 }
148 }
149
150 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
151 BOOST_THROW_EXCEPTION(ConfigFile::Error(
152 "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. "
153 "Remove face_system.udp section to disable UDP channels or enable at least one of them."));
154 }
155 }
156
Junxiao Shi79a92082017-08-08 02:40:59 +0000157 if (context.isDryRun) {
158 return;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000159 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000160
161 if (enableV4) {
162 udp::Endpoint endpoint(ip::udp::v4(), port);
163 shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
164 if (!v4Channel->isListening()) {
165 v4Channel->listen(this->addFace, nullptr);
166 }
167 providedSchemes.insert("udp");
168 providedSchemes.insert("udp4");
169 }
170 else if (providedSchemes.count("udp4") > 0) {
171 NFD_LOG_WARN("Cannot close udp4 channel after its creation");
172 }
173
174 if (enableV6) {
175 udp::Endpoint endpoint(ip::udp::v6(), port);
176 shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
177 if (!v6Channel->isListening()) {
178 v6Channel->listen(this->addFace, nullptr);
179 }
180 providedSchemes.insert("udp");
181 providedSchemes.insert("udp6");
182 }
183 else if (providedSchemes.count("udp6") > 0) {
184 NFD_LOG_WARN("Cannot close udp6 channel after its creation");
185 }
186
187 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
188 if (mcastConfig.isEnabled) {
189 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
190 }
191 else {
192 NFD_LOG_INFO("disabling multicast");
193 }
194 }
195 else if (mcastConfig.isEnabled) {
196 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
197 NFD_LOG_WARN("Cannot change ad hoc setting on existing faces");
198 }
199 if (m_mcastConfig.group != mcastConfig.group) {
200 NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group <<
201 " to " << mcastConfig.group);
202 }
203 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
204 NFD_LOG_INFO("changing whitelist/blacklist");
205 }
206 }
207
208 // Even if there's no configuration change, we still need to re-apply configuration because
209 // netifs may have changed.
210 m_mcastConfig = mcastConfig;
211 this->applyMcastConfig(context);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000212}
213
214void
Eric Newberry944f38b2017-07-20 20:54:22 -0400215UdpFactory::createFace(const CreateFaceParams& params,
Junxiao Shi64d99f22017-01-21 23:06:36 +0000216 const FaceCreatedCallback& onCreated,
217 const FaceCreationFailedCallback& onFailure)
218{
Eric Newberry944f38b2017-07-20 20:54:22 -0400219 BOOST_ASSERT(params.remoteUri.isCanonical());
Eric Newberry78e32b02017-04-01 14:34:44 +0000220
Eric Newberry944f38b2017-07-20 20:54:22 -0400221 if (params.localUri) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000222 NFD_LOG_TRACE("Cannot create unicast UDP face with LocalUri");
223 onFailure(406, "Unicast UDP faces cannot be created with a LocalUri");
224 return;
225 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000226
Eric Newberry944f38b2017-07-20 20:54:22 -0400227 if (params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000228 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
Davide Pesavento46afec42017-05-28 14:28:47 -0400229 onFailure(406, "Outgoing UDP faces do not support on-demand persistency");
Junxiao Shi64d99f22017-01-21 23:06:36 +0000230 return;
231 }
232
Yanbiao Liafb20592017-08-03 16:20:46 -0700233 udp::Endpoint endpoint(ndn::ip::addressFromString(params.remoteUri.getHost()),
Eric Newberry944f38b2017-07-20 20:54:22 -0400234 boost::lexical_cast<uint16_t>(params.remoteUri.getPort()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000235
236 if (endpoint.address().is_multicast()) {
237 NFD_LOG_TRACE("createFace does not support multicast faces");
238 onFailure(406, "Cannot create multicast UDP faces");
239 return;
240 }
241
Eric Newberry2642cd22017-07-13 21:34:53 -0400242 if (params.wantLocalFields) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000243 // UDP faces are never local
244 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
245 onFailure(406, "Local fields can only be enabled on faces with local scope");
246 return;
247 }
248
249 // very simple logic for now
250 for (const auto& i : m_channels) {
251 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
252 (i.first.address().is_v6() && endpoint.address().is_v6())) {
Eric Newberry2642cd22017-07-13 21:34:53 -0400253 i.second->connect(endpoint, params.persistency, params.wantLpReliability,
254 onCreated, onFailure);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000255 return;
256 }
257 }
258
Davide Pesavento46afec42017-05-28 14:28:47 -0400259 NFD_LOG_TRACE("No channels available to connect to " << endpoint);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000260 onFailure(504, "No channels available to connect");
261}
262
Giulio Grassi624f6c62014-02-18 19:42:14 +0100263shared_ptr<UdpChannel>
Davide Pesavento46afec42017-05-28 14:28:47 -0400264UdpFactory::createChannel(const udp::Endpoint& localEndpoint,
265 time::nanoseconds idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100266{
Davide Pesavento46afec42017-05-28 14:28:47 -0400267 auto it = m_channels.find(localEndpoint);
268 if (it != m_channels.end())
269 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100270
Davide Pesavento46afec42017-05-28 14:28:47 -0400271 if (localEndpoint.address().is_multicast()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200272 BOOST_THROW_EXCEPTION(Error("createChannel is only for unicast channels. The provided endpoint "
273 "is multicast. Use createMulticastFace to create a multicast face"));
274 }
275
Yukai Tu0a49d342015-09-13 12:54:22 +0800276 // check if the endpoint is already used by a multicast face
Davide Pesavento46afec42017-05-28 14:28:47 -0400277 if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700278 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP unicast channel, local "
279 "endpoint is already allocated for a UDP multicast face"));
Yukai Tu0a49d342015-09-13 12:54:22 +0800280 }
Junxiao Shi79494162014-04-02 18:25:11 -0700281
Davide Pesavento46afec42017-05-28 14:28:47 -0400282 auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout);
283 m_channels[localEndpoint] = channel;
Davide Pesavento46afec42017-05-28 14:28:47 -0400284
Giulio Grassi624f6c62014-02-18 19:42:14 +0100285 return channel;
286}
287
Junxiao Shi64d99f22017-01-21 23:06:36 +0000288std::vector<shared_ptr<const Channel>>
289UdpFactory::getChannels() const
290{
291 return getChannelsFromMap(m_channels);
292}
293
Junxiao Shicde37ad2015-12-24 01:02:05 -0700294shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100295UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200296 const udp::Endpoint& multicastEndpoint,
Davide Pesaventobb734df2017-10-24 18:05:36 -0400297 const net::NetworkInterface& netif)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100298{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400299 BOOST_ASSERT(multicastEndpoint.address().is_multicast());
300 BOOST_ASSERT(localEndpoint.port() == multicastEndpoint.port());
301
302 // check if the local and multicast endpoints are already in use for a multicast face
Davide Pesavento46afec42017-05-28 14:28:47 -0400303 auto it = m_mcastFaces.find(localEndpoint);
304 if (it != m_mcastFaces.end()) {
305 if (it->second->getRemoteUri() == FaceUri(multicastEndpoint))
306 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100307 else
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700308 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local "
309 "endpoint is already allocated for a UDP multicast face "
310 "on a different multicast group"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100311 }
Junxiao Shi79494162014-04-02 18:25:11 -0700312
Davide Pesaventobb734df2017-10-24 18:05:36 -0400313 // check if the local endpoint is already used by a unicast channel
Davide Pesavento46afec42017-05-28 14:28:47 -0400314 if (m_channels.find(localEndpoint) != m_channels.end()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700315 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local "
316 "endpoint is already allocated for a UDP unicast channel"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100317 }
318
319 if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700320 BOOST_THROW_EXCEPTION(Error("IPv6 multicast is not supported yet. Please provide an IPv4 "
321 "address"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100322 }
Junxiao Shi79494162014-04-02 18:25:11 -0700323
Davide Pesavento292e5e12015-03-13 02:08:33 +0100324 ip::udp::socket receiveSocket(getGlobalIoService());
325 receiveSocket.open(multicastEndpoint.protocol());
326 receiveSocket.set_option(ip::udp::socket::reuse_address(true));
327 receiveSocket.bind(multicastEndpoint);
Junxiao Shi79494162014-04-02 18:25:11 -0700328
Davide Pesavento292e5e12015-03-13 02:08:33 +0100329 ip::udp::socket sendSocket(getGlobalIoService());
330 sendSocket.open(multicastEndpoint.protocol());
331 sendSocket.set_option(ip::udp::socket::reuse_address(true));
332 sendSocket.set_option(ip::multicast::enable_loopback(false));
333 sendSocket.bind(udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port()));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200334 if (localEndpoint.address() != ip::address_v4::any())
Davide Pesavento292e5e12015-03-13 02:08:33 +0100335 sendSocket.set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100336
Davide Pesavento292e5e12015-03-13 02:08:33 +0100337 sendSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
338 localEndpoint.address().to_v4()));
339 receiveSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700340 localEndpoint.address().to_v4()));
341
Davide Pesavento292e5e12015-03-13 02:08:33 +0100342#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000343 // On Linux, if there is more than one multicast UDP face for the same multicast
344 // group but they are bound to different network interfaces, the socket needs
345 // to be bound to the specific interface using SO_BINDTODEVICE, otherwise the
346 // face will receive all packets sent to the other interfaces as well.
347 // This happens only on Linux. On macOS, the ip::multicast::join_group option
348 // is enough to get the desired behaviour.
Davide Pesaventobb734df2017-10-24 18:05:36 -0400349 if (::setsockopt(receiveSocket.native_handle(), SOL_SOCKET, SO_BINDTODEVICE,
350 netif.getName().data(), netif.getName().size() + 1) < 0) {
351 BOOST_THROW_EXCEPTION(Error("Cannot bind multicast face to " + netif.getName() +
352 ": " + std::strerror(errno)));
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200353 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000354#endif // __linux__
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200355
Junxiao Shi64d99f22017-01-21 23:06:36 +0000356 auto linkService = make_unique<GenericLinkService>();
357 auto transport = make_unique<MulticastUdpTransport>(localEndpoint, multicastEndpoint,
358 std::move(receiveSocket),
Teng Liangfe4fce32017-03-29 04:49:38 +0000359 std::move(sendSocket),
360 m_mcastConfig.linkType);
Davide Pesavento46afec42017-05-28 14:28:47 -0400361 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700362
Junxiao Shi64d99f22017-01-21 23:06:36 +0000363 m_mcastFaces[localEndpoint] = face;
364 connectFaceClosedSignal(*face, [this, localEndpoint] { m_mcastFaces.erase(localEndpoint); });
Giulio Grassi624f6c62014-02-18 19:42:14 +0100365
Davide Pesavento292e5e12015-03-13 02:08:33 +0100366 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100367}
368
Davide Pesaventobb734df2017-10-24 18:05:36 -0400369static ndn::optional<ip::address>
370getV4Address(const net::NetworkInterface& netif)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100371{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400372 for (const auto& na : netif.getNetworkAddresses()) {
373 if (na.getFamily() == net::AddressFamily::V4 && na.getScope() != net::AddressScope::NOWHERE) {
374 return na.getIp();
Junxiao Shibbace1d2017-08-06 20:03:37 +0000375 }
376 }
377 return ndn::nullopt;
378}
379
380shared_ptr<Face>
Davide Pesaventobb734df2017-10-24 18:05:36 -0400381UdpFactory::applyMcastConfigToNetif(const shared_ptr<const net::NetworkInterface>& netif)
Junxiao Shibbace1d2017-08-06 20:03:37 +0000382{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400383 BOOST_ASSERT(netif != nullptr);
384
Junxiao Shibbace1d2017-08-06 20:03:37 +0000385 if (!m_mcastConfig.isEnabled) {
386 return nullptr;
387 }
388
389 if (!netif->isUp()) {
390 NFD_LOG_DEBUG("Not creating multicast face on " << netif->getName() << ": netif is down");
391 return nullptr;
392 }
393
394 if (!netif->canMulticast()) {
395 NFD_LOG_DEBUG("Not creating multicast face on " << netif->getName() << ": netif cannot multicast");
396 return nullptr;
397 }
398
399 auto address = getV4Address(*netif);
400 if (!address) {
401 NFD_LOG_DEBUG("Not creating multicast face on " << netif->getName() << ": no IPv4 address");
402 // keep an eye on new addresses
403 m_netifConns[netif->getIndex()].addrAddConn =
404 netif->onAddressAdded.connectSingleShot(bind(&UdpFactory::applyMcastConfigToNetif, this, netif));
405 return nullptr;
406 }
407
408 if (!m_mcastConfig.netifPredicate(*netif)) {
Junxiao Shi79a92082017-08-08 02:40:59 +0000409 NFD_LOG_DEBUG("Not creating multicast face on " << netif->getName() << ": rejected by whitelist/blacklist");
Junxiao Shibbace1d2017-08-06 20:03:37 +0000410 return nullptr;
411 }
412
413 NFD_LOG_DEBUG("Creating multicast face on " << netif->getName());
Davide Pesaventobb734df2017-10-24 18:05:36 -0400414 udp::Endpoint localEndpoint(*address, m_mcastConfig.group.port());
415 auto face = this->createMulticastFace(localEndpoint, m_mcastConfig.group, *netif);
Junxiao Shi79a92082017-08-08 02:40:59 +0000416 // ifname is only used on Linux. It is not required if there is only one multicast-capable netif,
417 // but it is always supplied because a new netif can be added at anytime.
Junxiao Shibbace1d2017-08-06 20:03:37 +0000418
419 if (face->getId() == INVALID_FACEID) {
420 // new face: register with forwarding
421 this->addFace(face);
422 }
423 return face;
424}
425
Junxiao Shi64d99f22017-01-21 23:06:36 +0000426void
Junxiao Shibbace1d2017-08-06 20:03:37 +0000427UdpFactory::applyMcastConfig(const FaceSystem::ConfigContext& context)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000428{
429 // collect old faces
430 std::set<shared_ptr<Face>> oldFaces;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000431 boost::copy(m_mcastFaces | boost::adaptors::map_values, std::inserter(oldFaces, oldFaces.end()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000432
Junxiao Shibbace1d2017-08-06 20:03:37 +0000433 // create faces if requested by config
434 for (const auto& netif : netmon->listNetworkInterfaces()) {
435 auto face = this->applyMcastConfigToNetif(netif);
436 if (face != nullptr) {
437 // don't destroy face
438 oldFaces.erase(face);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000439 }
440 }
441
442 // destroy old faces that are not needed in new configuration
443 for (const auto& face : oldFaces) {
444 face->close();
445 }
446}
447
448} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100449} // namespace nfd