blob: 2024cd664c19a76b987238aa46e8e6d76d638a7d [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/*
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05003 * Copyright (c) 2014-2018, 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
35namespace nfd {
Junxiao Shi64d99f22017-01-21 23:06:36 +000036namespace face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010037
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020038namespace ip = boost::asio::ip;
Davide Pesaventobb734df2017-10-24 18:05:36 -040039namespace net = ndn::net;
Junxiao Shi79494162014-04-02 18:25:11 -070040
Giulio Grassi624f6c62014-02-18 19:42:14 +010041NFD_LOG_INIT("UdpFactory");
Junxiao Shib47247d2017-01-24 15:09:16 +000042NFD_REGISTER_PROTOCOL_FACTORY(UdpFactory);
43
44const std::string&
45UdpFactory::getId()
46{
47 static std::string id("udp");
48 return id;
49}
50
Junxiao Shi0ba6d642017-07-17 00:53:22 +000051UdpFactory::UdpFactory(const CtorParams& params)
52 : ProtocolFactory(params)
53{
Junxiao Shibbace1d2017-08-06 20:03:37 +000054 m_netifAddConn = netmon->onInterfaceAdded.connect(bind(&UdpFactory::applyMcastConfigToNetif, this, _1));
Junxiao Shi0ba6d642017-07-17 00:53:22 +000055}
Giulio Grassi624f6c62014-02-18 19:42:14 +010056
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060057void
Junxiao Shi64d99f22017-01-21 23:06:36 +000058UdpFactory::processConfig(OptionalConfigSection configSection,
59 FaceSystem::ConfigContext& context)
60{
61 // udp
62 // {
63 // port 6363
64 // enable_v4 yes
65 // enable_v6 yes
66 // idle_timeout 600
67 // keep_alive_interval 25 ; acceptable but ignored
68 // mcast yes
69 // mcast_group 224.0.23.170
70 // mcast_port 56363
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050071 // mcast_group_v6 ff02::1234
72 // mcast_port_v6 56363
Teng Liangfe4fce32017-03-29 04:49:38 +000073 // mcast_ad_hoc no
Junxiao Shic31080d2017-01-24 15:10:12 +000074 // whitelist
75 // {
76 // *
77 // }
78 // blacklist
79 // {
80 // }
Junxiao Shi64d99f22017-01-21 23:06:36 +000081 // }
82
83 uint16_t port = 6363;
84 bool enableV4 = false;
85 bool enableV6 = false;
86 uint32_t idleTimeout = 600;
87 MulticastConfig mcastConfig;
88
89 if (configSection) {
90 // These default to 'yes' but only if face_system.udp section is present
91 enableV4 = enableV6 = mcastConfig.isEnabled = true;
92
93 for (const auto& pair : *configSection) {
94 const std::string& key = pair.first;
95 const ConfigSection& value = pair.second;
96
97 if (key == "port") {
98 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp");
99 }
100 else if (key == "enable_v4") {
101 enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp");
102 }
103 else if (key == "enable_v6") {
104 enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp");
105 }
106 else if (key == "idle_timeout") {
107 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp");
108 }
109 else if (key == "keep_alive_interval") {
110 // ignored
111 }
112 else if (key == "mcast") {
113 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp");
114 }
115 else if (key == "mcast_group") {
116 const std::string& valueStr = value.get_value<std::string>();
117 boost::system::error_code ec;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400118 mcastConfig.group.address(ip::address_v4::from_string(valueStr, ec));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000119 if (ec) {
120 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
121 valueStr + "' cannot be parsed as an IPv4 address"));
122 }
123 else if (!mcastConfig.group.address().is_multicast()) {
124 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
125 valueStr + "' is not a multicast address"));
126 }
127 }
128 else if (key == "mcast_port") {
129 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
130 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500131 else if (key == "mcast_group_v6") {
132 const std::string& valueStr = value.get_value<std::string>();
133 boost::system::error_code ec;
134 mcastConfig.groupV6.address(ndn::ip::addressV6FromString(valueStr, ec));
135 if (ec) {
136 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
137 valueStr + "' cannot be parsed as an IPv6 address"));
138 }
139 else if (!mcastConfig.groupV6.address().is_multicast()) {
140 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
141 valueStr + "' is not a multicast address"));
142 }
143 }
144 else if (key == "mcast_port_v6") {
145 mcastConfig.groupV6.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
146 }
Teng Liangfe4fce32017-03-29 04:49:38 +0000147 else if (key == "mcast_ad_hoc") {
148 bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.udp");
149 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
150 }
Junxiao Shic31080d2017-01-24 15:10:12 +0000151 else if (key == "whitelist") {
152 mcastConfig.netifPredicate.parseWhitelist(value);
153 }
154 else if (key == "blacklist") {
155 mcastConfig.netifPredicate.parseBlacklist(value);
156 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000157 else {
158 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.udp." + key));
159 }
160 }
161
162 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
163 BOOST_THROW_EXCEPTION(ConfigFile::Error(
164 "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. "
165 "Remove face_system.udp section to disable UDP channels or enable at least one of them."));
166 }
167 }
168
Junxiao Shi79a92082017-08-08 02:40:59 +0000169 if (context.isDryRun) {
170 return;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000171 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000172
173 if (enableV4) {
174 udp::Endpoint endpoint(ip::udp::v4(), port);
175 shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
176 if (!v4Channel->isListening()) {
177 v4Channel->listen(this->addFace, nullptr);
178 }
179 providedSchemes.insert("udp");
180 providedSchemes.insert("udp4");
181 }
182 else if (providedSchemes.count("udp4") > 0) {
183 NFD_LOG_WARN("Cannot close udp4 channel after its creation");
184 }
185
186 if (enableV6) {
187 udp::Endpoint endpoint(ip::udp::v6(), port);
188 shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
189 if (!v6Channel->isListening()) {
190 v6Channel->listen(this->addFace, nullptr);
191 }
192 providedSchemes.insert("udp");
193 providedSchemes.insert("udp6");
194 }
195 else if (providedSchemes.count("udp6") > 0) {
196 NFD_LOG_WARN("Cannot close udp6 channel after its creation");
197 }
198
199 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
200 if (mcastConfig.isEnabled) {
201 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500202 NFD_LOG_INFO("enabling multicast on " << mcastConfig.groupV6);
Junxiao Shi79a92082017-08-08 02:40:59 +0000203 }
204 else {
205 NFD_LOG_INFO("disabling multicast");
206 }
207 }
208 else if (mcastConfig.isEnabled) {
209 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
210 NFD_LOG_WARN("Cannot change ad hoc setting on existing faces");
211 }
212 if (m_mcastConfig.group != mcastConfig.group) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500213 NFD_LOG_INFO("changing IPv4 multicast group from " << m_mcastConfig.group <<
Junxiao Shi79a92082017-08-08 02:40:59 +0000214 " to " << mcastConfig.group);
215 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500216 if (m_mcastConfig.groupV6 != mcastConfig.groupV6) {
217 NFD_LOG_INFO("changing IPv6 multicast group from " << m_mcastConfig.groupV6 <<
218 " to " << mcastConfig.groupV6);
219 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000220 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
221 NFD_LOG_INFO("changing whitelist/blacklist");
222 }
223 }
224
225 // Even if there's no configuration change, we still need to re-apply configuration because
226 // netifs may have changed.
227 m_mcastConfig = mcastConfig;
228 this->applyMcastConfig(context);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000229}
230
231void
Eric Newberry944f38b2017-07-20 20:54:22 -0400232UdpFactory::createFace(const CreateFaceParams& params,
Junxiao Shi64d99f22017-01-21 23:06:36 +0000233 const FaceCreatedCallback& onCreated,
234 const FaceCreationFailedCallback& onFailure)
235{
Eric Newberry944f38b2017-07-20 20:54:22 -0400236 BOOST_ASSERT(params.remoteUri.isCanonical());
Eric Newberry78e32b02017-04-01 14:34:44 +0000237
Eric Newberry944f38b2017-07-20 20:54:22 -0400238 if (params.localUri) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000239 NFD_LOG_TRACE("Cannot create unicast UDP face with LocalUri");
240 onFailure(406, "Unicast UDP faces cannot be created with a LocalUri");
241 return;
242 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000243
Eric Newberry944f38b2017-07-20 20:54:22 -0400244 if (params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000245 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
Davide Pesavento46afec42017-05-28 14:28:47 -0400246 onFailure(406, "Outgoing UDP faces do not support on-demand persistency");
Junxiao Shi64d99f22017-01-21 23:06:36 +0000247 return;
248 }
249
Yanbiao Liafb20592017-08-03 16:20:46 -0700250 udp::Endpoint endpoint(ndn::ip::addressFromString(params.remoteUri.getHost()),
Eric Newberry944f38b2017-07-20 20:54:22 -0400251 boost::lexical_cast<uint16_t>(params.remoteUri.getPort()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000252
253 if (endpoint.address().is_multicast()) {
254 NFD_LOG_TRACE("createFace does not support multicast faces");
255 onFailure(406, "Cannot create multicast UDP faces");
256 return;
257 }
258
Eric Newberry2642cd22017-07-13 21:34:53 -0400259 if (params.wantLocalFields) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000260 // UDP faces are never local
261 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
262 onFailure(406, "Local fields can only be enabled on faces with local scope");
263 return;
264 }
265
266 // very simple logic for now
267 for (const auto& i : m_channels) {
268 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
269 (i.first.address().is_v6() && endpoint.address().is_v6())) {
Eric Newberry2642cd22017-07-13 21:34:53 -0400270 i.second->connect(endpoint, params.persistency, params.wantLpReliability,
271 onCreated, onFailure);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000272 return;
273 }
274 }
275
Davide Pesavento46afec42017-05-28 14:28:47 -0400276 NFD_LOG_TRACE("No channels available to connect to " << endpoint);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000277 onFailure(504, "No channels available to connect");
278}
279
Giulio Grassi624f6c62014-02-18 19:42:14 +0100280shared_ptr<UdpChannel>
Davide Pesavento46afec42017-05-28 14:28:47 -0400281UdpFactory::createChannel(const udp::Endpoint& localEndpoint,
282 time::nanoseconds idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100283{
Davide Pesavento46afec42017-05-28 14:28:47 -0400284 auto it = m_channels.find(localEndpoint);
285 if (it != m_channels.end())
286 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100287
Yukai Tu0a49d342015-09-13 12:54:22 +0800288 // check if the endpoint is already used by a multicast face
Davide Pesavento46afec42017-05-28 14:28:47 -0400289 if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500290 BOOST_THROW_EXCEPTION(Error("Cannot create UDP channel on " +
291 boost::lexical_cast<std::string>(localEndpoint) +
292 ", endpoint already allocated for a UDP multicast face"));
Yukai Tu0a49d342015-09-13 12:54:22 +0800293 }
Junxiao Shi79494162014-04-02 18:25:11 -0700294
Davide Pesavento46afec42017-05-28 14:28:47 -0400295 auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout);
296 m_channels[localEndpoint] = channel;
Davide Pesavento46afec42017-05-28 14:28:47 -0400297
Giulio Grassi624f6c62014-02-18 19:42:14 +0100298 return channel;
299}
300
Junxiao Shi64d99f22017-01-21 23:06:36 +0000301std::vector<shared_ptr<const Channel>>
302UdpFactory::getChannels() const
303{
304 return getChannelsFromMap(m_channels);
305}
306
Junxiao Shicde37ad2015-12-24 01:02:05 -0700307shared_ptr<Face>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500308UdpFactory::createMulticastFace(const shared_ptr<const net::NetworkInterface>& netif,
309 const ip::address& localAddress,
310 const udp::Endpoint& multicastEndpoint)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100311{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400312 BOOST_ASSERT(multicastEndpoint.address().is_multicast());
Davide Pesaventobb734df2017-10-24 18:05:36 -0400313
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500314 udp::Endpoint localEp(localAddress, multicastEndpoint.port());
315 BOOST_ASSERT(localEp.protocol() == multicastEndpoint.protocol());
316
317 auto mcastEp = multicastEndpoint;
318 if (mcastEp.address().is_v6()) {
319 // in IPv6, a scope id on the multicast address is always required
320 auto mcastAddress = mcastEp.address().to_v6();
321 mcastAddress.scope_id(netif->getIndex());
322 mcastEp.address(mcastAddress);
323 }
324
325 // check if the local endpoint is already used by another multicast face
326 auto it = m_mcastFaces.find(localEp);
Davide Pesavento46afec42017-05-28 14:28:47 -0400327 if (it != m_mcastFaces.end()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500328 if (it->second->getRemoteUri() == FaceUri(mcastEp))
Davide Pesavento46afec42017-05-28 14:28:47 -0400329 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100330 else
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500331 BOOST_THROW_EXCEPTION(Error("Cannot create UDP multicast face on " +
332 boost::lexical_cast<std::string>(localEp) +
333 ", endpoint already allocated for a different UDP multicast face"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100334 }
Junxiao Shi79494162014-04-02 18:25:11 -0700335
Davide Pesaventobb734df2017-10-24 18:05:36 -0400336 // check if the local endpoint is already used by a unicast channel
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500337 if (m_channels.find(localEp) != m_channels.end()) {
338 BOOST_THROW_EXCEPTION(Error("Cannot create UDP multicast face on " +
339 boost::lexical_cast<std::string>(localEp) +
340 ", endpoint already allocated for a UDP channel"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100341 }
Junxiao Shi79494162014-04-02 18:25:11 -0700342
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500343 ip::udp::socket rxSock(getGlobalIoService());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500344 MulticastUdpTransport::openRxSocket(rxSock, mcastEp, localAddress, netif);
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500345 ip::udp::socket txSock(getGlobalIoService());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500346 MulticastUdpTransport::openTxSocket(txSock, udp::Endpoint(localAddress, 0), netif);
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200347
Junxiao Shi64d99f22017-01-21 23:06:36 +0000348 auto linkService = make_unique<GenericLinkService>();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500349 auto transport = make_unique<MulticastUdpTransport>(mcastEp, std::move(rxSock), std::move(txSock),
Teng Liangfe4fce32017-03-29 04:49:38 +0000350 m_mcastConfig.linkType);
Davide Pesavento46afec42017-05-28 14:28:47 -0400351 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700352
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500353 m_mcastFaces[localEp] = face;
354 connectFaceClosedSignal(*face, [this, localEp] { m_mcastFaces.erase(localEp); });
Giulio Grassi624f6c62014-02-18 19:42:14 +0100355
Davide Pesavento292e5e12015-03-13 02:08:33 +0100356 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100357}
358
Davide Pesaventobb734df2017-10-24 18:05:36 -0400359static ndn::optional<ip::address>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500360pickAddress(const net::NetworkInterface& netif, net::AddressFamily af)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100361{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400362 for (const auto& na : netif.getNetworkAddresses()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500363 if (na.getFamily() == af &&
364 (na.getScope() == net::AddressScope::LINK || na.getScope() == net::AddressScope::GLOBAL)) {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400365 return na.getIp();
Junxiao Shibbace1d2017-08-06 20:03:37 +0000366 }
367 }
368 return ndn::nullopt;
369}
370
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500371std::vector<shared_ptr<Face>>
Davide Pesaventobb734df2017-10-24 18:05:36 -0400372UdpFactory::applyMcastConfigToNetif(const shared_ptr<const net::NetworkInterface>& netif)
Junxiao Shibbace1d2017-08-06 20:03:37 +0000373{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400374 BOOST_ASSERT(netif != nullptr);
375
Junxiao Shibbace1d2017-08-06 20:03:37 +0000376 if (!m_mcastConfig.isEnabled) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500377 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000378 }
379
380 if (!netif->isUp()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500381 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is down");
382 return {};
383 }
384
385 if (netif->isLoopback()) {
386 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is loopback");
387 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000388 }
389
390 if (!netif->canMulticast()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500391 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif cannot multicast");
392 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000393 }
394
395 if (!m_mcastConfig.netifPredicate(*netif)) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500396 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": rejected by whitelist/blacklist");
397 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000398 }
399
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500400 std::vector<ip::address> addrs;
401 for (auto af : {net::AddressFamily::V4, net::AddressFamily::V6}) {
402 auto addr = pickAddress(*netif, af);
403 if (addr)
404 addrs.push_back(*addr);
Junxiao Shibbace1d2017-08-06 20:03:37 +0000405 }
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500406
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500407 if (addrs.empty()) {
408 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": no viable IP address");
409 // keep an eye on new addresses
410 m_netifConns[netif->getIndex()].addrAddConn =
411 netif->onAddressAdded.connect(bind(&UdpFactory::applyMcastConfigToNetif, this, netif));
412 return {};
413 }
414
415 NFD_LOG_DEBUG("Creating multicast faces on " << netif->getName());
416
417 std::vector<shared_ptr<Face>> faces;
418 for (const auto& addr : addrs) {
419 auto face = this->createMulticastFace(netif, addr,
420 addr.is_v4() ? m_mcastConfig.group : m_mcastConfig.groupV6);
421 if (face->getId() == INVALID_FACEID) {
422 // new face: register with forwarding
423 this->addFace(face);
424 }
425 faces.push_back(std::move(face));
426 }
427
428 return faces;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000429}
430
Junxiao Shi64d99f22017-01-21 23:06:36 +0000431void
Junxiao Shibbace1d2017-08-06 20:03:37 +0000432UdpFactory::applyMcastConfig(const FaceSystem::ConfigContext& context)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000433{
434 // collect old faces
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500435 std::set<shared_ptr<Face>> facesToClose;
436 boost::copy(m_mcastFaces | boost::adaptors::map_values,
437 std::inserter(facesToClose, facesToClose.end()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000438
Junxiao Shibbace1d2017-08-06 20:03:37 +0000439 // create faces if requested by config
440 for (const auto& netif : netmon->listNetworkInterfaces()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500441 auto facesToKeep = this->applyMcastConfigToNetif(netif);
442 for (const auto& face : facesToKeep) {
Junxiao Shibbace1d2017-08-06 20:03:37 +0000443 // don't destroy face
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500444 facesToClose.erase(face);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000445 }
446 }
447
448 // destroy old faces that are not needed in new configuration
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500449 for (const auto& face : facesToClose) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000450 face->close();
451 }
452}
453
454} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100455} // namespace nfd