blob: 74b697498bc8edd711ef7e03f8bd961273bbde09 [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
Eric Newberry0c841642018-01-17 15:01:00 -070083 m_wantCongestionMarking = context.generalConfig.wantCongestionMarking;
84
Junxiao Shi64d99f22017-01-21 23:06:36 +000085 uint16_t port = 6363;
86 bool enableV4 = false;
87 bool enableV6 = false;
88 uint32_t idleTimeout = 600;
89 MulticastConfig mcastConfig;
90
91 if (configSection) {
92 // These default to 'yes' but only if face_system.udp section is present
93 enableV4 = enableV6 = mcastConfig.isEnabled = true;
94
95 for (const auto& pair : *configSection) {
96 const std::string& key = pair.first;
97 const ConfigSection& value = pair.second;
98
99 if (key == "port") {
100 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp");
101 }
102 else if (key == "enable_v4") {
103 enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp");
104 }
105 else if (key == "enable_v6") {
106 enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp");
107 }
108 else if (key == "idle_timeout") {
109 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp");
110 }
111 else if (key == "keep_alive_interval") {
112 // ignored
113 }
114 else if (key == "mcast") {
115 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp");
116 }
117 else if (key == "mcast_group") {
118 const std::string& valueStr = value.get_value<std::string>();
119 boost::system::error_code ec;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400120 mcastConfig.group.address(ip::address_v4::from_string(valueStr, ec));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000121 if (ec) {
122 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
123 valueStr + "' cannot be parsed as an IPv4 address"));
124 }
125 else if (!mcastConfig.group.address().is_multicast()) {
126 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
127 valueStr + "' is not a multicast address"));
128 }
129 }
130 else if (key == "mcast_port") {
131 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
132 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500133 else if (key == "mcast_group_v6") {
134 const std::string& valueStr = value.get_value<std::string>();
135 boost::system::error_code ec;
136 mcastConfig.groupV6.address(ndn::ip::addressV6FromString(valueStr, ec));
137 if (ec) {
138 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
139 valueStr + "' cannot be parsed as an IPv6 address"));
140 }
141 else if (!mcastConfig.groupV6.address().is_multicast()) {
142 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
143 valueStr + "' is not a multicast address"));
144 }
145 }
146 else if (key == "mcast_port_v6") {
147 mcastConfig.groupV6.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
148 }
Teng Liangfe4fce32017-03-29 04:49:38 +0000149 else if (key == "mcast_ad_hoc") {
150 bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.udp");
151 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
152 }
Junxiao Shic31080d2017-01-24 15:10:12 +0000153 else if (key == "whitelist") {
154 mcastConfig.netifPredicate.parseWhitelist(value);
155 }
156 else if (key == "blacklist") {
157 mcastConfig.netifPredicate.parseBlacklist(value);
158 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000159 else {
160 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.udp." + key));
161 }
162 }
163
164 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
165 BOOST_THROW_EXCEPTION(ConfigFile::Error(
166 "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. "
167 "Remove face_system.udp section to disable UDP channels or enable at least one of them."));
168 }
169 }
170
Junxiao Shi79a92082017-08-08 02:40:59 +0000171 if (context.isDryRun) {
172 return;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000173 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000174
175 if (enableV4) {
176 udp::Endpoint endpoint(ip::udp::v4(), port);
177 shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
178 if (!v4Channel->isListening()) {
179 v4Channel->listen(this->addFace, nullptr);
180 }
181 providedSchemes.insert("udp");
182 providedSchemes.insert("udp4");
183 }
184 else if (providedSchemes.count("udp4") > 0) {
185 NFD_LOG_WARN("Cannot close udp4 channel after its creation");
186 }
187
188 if (enableV6) {
189 udp::Endpoint endpoint(ip::udp::v6(), port);
190 shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
191 if (!v6Channel->isListening()) {
192 v6Channel->listen(this->addFace, nullptr);
193 }
194 providedSchemes.insert("udp");
195 providedSchemes.insert("udp6");
196 }
197 else if (providedSchemes.count("udp6") > 0) {
198 NFD_LOG_WARN("Cannot close udp6 channel after its creation");
199 }
200
201 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
202 if (mcastConfig.isEnabled) {
203 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500204 NFD_LOG_INFO("enabling multicast on " << mcastConfig.groupV6);
Junxiao Shi79a92082017-08-08 02:40:59 +0000205 }
206 else {
207 NFD_LOG_INFO("disabling multicast");
208 }
209 }
210 else if (mcastConfig.isEnabled) {
211 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
212 NFD_LOG_WARN("Cannot change ad hoc setting on existing faces");
213 }
214 if (m_mcastConfig.group != mcastConfig.group) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500215 NFD_LOG_INFO("changing IPv4 multicast group from " << m_mcastConfig.group <<
Junxiao Shi79a92082017-08-08 02:40:59 +0000216 " to " << mcastConfig.group);
217 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500218 if (m_mcastConfig.groupV6 != mcastConfig.groupV6) {
219 NFD_LOG_INFO("changing IPv6 multicast group from " << m_mcastConfig.groupV6 <<
220 " to " << mcastConfig.groupV6);
221 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000222 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
223 NFD_LOG_INFO("changing whitelist/blacklist");
224 }
225 }
226
227 // Even if there's no configuration change, we still need to re-apply configuration because
228 // netifs may have changed.
229 m_mcastConfig = mcastConfig;
230 this->applyMcastConfig(context);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000231}
232
233void
Davide Pesavento15b55052018-01-27 19:09:28 -0500234UdpFactory::createFace(const CreateFaceRequest& req,
Junxiao Shi64d99f22017-01-21 23:06:36 +0000235 const FaceCreatedCallback& onCreated,
236 const FaceCreationFailedCallback& onFailure)
237{
Davide Pesavento15b55052018-01-27 19:09:28 -0500238 BOOST_ASSERT(req.remoteUri.isCanonical());
Eric Newberry78e32b02017-04-01 14:34:44 +0000239
Davide Pesavento15b55052018-01-27 19:09:28 -0500240 if (req.localUri) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000241 NFD_LOG_TRACE("Cannot create unicast UDP face with LocalUri");
242 onFailure(406, "Unicast UDP faces cannot be created with a LocalUri");
243 return;
244 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000245
Davide Pesavento15b55052018-01-27 19:09:28 -0500246 if (req.params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000247 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
Davide Pesavento46afec42017-05-28 14:28:47 -0400248 onFailure(406, "Outgoing UDP faces do not support on-demand persistency");
Junxiao Shi64d99f22017-01-21 23:06:36 +0000249 return;
250 }
251
Davide Pesavento15b55052018-01-27 19:09:28 -0500252 udp::Endpoint endpoint(ndn::ip::addressFromString(req.remoteUri.getHost()),
253 boost::lexical_cast<uint16_t>(req.remoteUri.getPort()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000254
255 if (endpoint.address().is_multicast()) {
256 NFD_LOG_TRACE("createFace does not support multicast faces");
257 onFailure(406, "Cannot create multicast UDP faces");
258 return;
259 }
260
Davide Pesavento15b55052018-01-27 19:09:28 -0500261 if (req.params.wantLocalFields) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000262 // UDP faces are never local
263 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
264 onFailure(406, "Local fields can only be enabled on faces with local scope");
265 return;
266 }
267
268 // very simple logic for now
269 for (const auto& i : m_channels) {
270 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
271 (i.first.address().is_v6() && endpoint.address().is_v6())) {
Davide Pesavento15b55052018-01-27 19:09:28 -0500272 i.second->connect(endpoint, req.params, onCreated, onFailure);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000273 return;
274 }
275 }
276
Davide Pesavento46afec42017-05-28 14:28:47 -0400277 NFD_LOG_TRACE("No channels available to connect to " << endpoint);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000278 onFailure(504, "No channels available to connect");
279}
280
Giulio Grassi624f6c62014-02-18 19:42:14 +0100281shared_ptr<UdpChannel>
Davide Pesavento46afec42017-05-28 14:28:47 -0400282UdpFactory::createChannel(const udp::Endpoint& localEndpoint,
283 time::nanoseconds idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100284{
Davide Pesavento46afec42017-05-28 14:28:47 -0400285 auto it = m_channels.find(localEndpoint);
286 if (it != m_channels.end())
287 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100288
Yukai Tu0a49d342015-09-13 12:54:22 +0800289 // check if the endpoint is already used by a multicast face
Davide Pesavento46afec42017-05-28 14:28:47 -0400290 if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500291 BOOST_THROW_EXCEPTION(Error("Cannot create UDP channel on " +
292 boost::lexical_cast<std::string>(localEndpoint) +
293 ", endpoint already allocated for a UDP multicast face"));
Yukai Tu0a49d342015-09-13 12:54:22 +0800294 }
Junxiao Shi79494162014-04-02 18:25:11 -0700295
Eric Newberry0c841642018-01-17 15:01:00 -0700296 auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout, m_wantCongestionMarking);
Davide Pesavento46afec42017-05-28 14:28:47 -0400297 m_channels[localEndpoint] = channel;
Davide Pesavento46afec42017-05-28 14:28:47 -0400298
Giulio Grassi624f6c62014-02-18 19:42:14 +0100299 return channel;
300}
301
Junxiao Shi64d99f22017-01-21 23:06:36 +0000302std::vector<shared_ptr<const Channel>>
303UdpFactory::getChannels() const
304{
305 return getChannelsFromMap(m_channels);
306}
307
Junxiao Shicde37ad2015-12-24 01:02:05 -0700308shared_ptr<Face>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500309UdpFactory::createMulticastFace(const shared_ptr<const net::NetworkInterface>& netif,
310 const ip::address& localAddress,
311 const udp::Endpoint& multicastEndpoint)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100312{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400313 BOOST_ASSERT(multicastEndpoint.address().is_multicast());
Davide Pesaventobb734df2017-10-24 18:05:36 -0400314
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500315 udp::Endpoint localEp(localAddress, multicastEndpoint.port());
316 BOOST_ASSERT(localEp.protocol() == multicastEndpoint.protocol());
317
318 auto mcastEp = multicastEndpoint;
319 if (mcastEp.address().is_v6()) {
320 // in IPv6, a scope id on the multicast address is always required
321 auto mcastAddress = mcastEp.address().to_v6();
322 mcastAddress.scope_id(netif->getIndex());
323 mcastEp.address(mcastAddress);
324 }
325
326 // check if the local endpoint is already used by another multicast face
327 auto it = m_mcastFaces.find(localEp);
Davide Pesavento46afec42017-05-28 14:28:47 -0400328 if (it != m_mcastFaces.end()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500329 if (it->second->getRemoteUri() == FaceUri(mcastEp))
Davide Pesavento46afec42017-05-28 14:28:47 -0400330 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100331 else
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500332 BOOST_THROW_EXCEPTION(Error("Cannot create UDP multicast face on " +
333 boost::lexical_cast<std::string>(localEp) +
334 ", endpoint already allocated for a different UDP multicast face"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100335 }
Junxiao Shi79494162014-04-02 18:25:11 -0700336
Davide Pesaventobb734df2017-10-24 18:05:36 -0400337 // check if the local endpoint is already used by a unicast channel
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500338 if (m_channels.find(localEp) != m_channels.end()) {
339 BOOST_THROW_EXCEPTION(Error("Cannot create UDP multicast face on " +
340 boost::lexical_cast<std::string>(localEp) +
341 ", endpoint already allocated for a UDP channel"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100342 }
Junxiao Shi79494162014-04-02 18:25:11 -0700343
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500344 ip::udp::socket rxSock(getGlobalIoService());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500345 MulticastUdpTransport::openRxSocket(rxSock, mcastEp, localAddress, netif);
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500346 ip::udp::socket txSock(getGlobalIoService());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500347 MulticastUdpTransport::openTxSocket(txSock, udp::Endpoint(localAddress, 0), netif);
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200348
Eric Newberry0c841642018-01-17 15:01:00 -0700349 GenericLinkService::Options options;
350 options.allowCongestionMarking = m_wantCongestionMarking;
351 auto linkService = make_unique<GenericLinkService>(options);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500352 auto transport = make_unique<MulticastUdpTransport>(mcastEp, std::move(rxSock), std::move(txSock),
Teng Liangfe4fce32017-03-29 04:49:38 +0000353 m_mcastConfig.linkType);
Davide Pesavento46afec42017-05-28 14:28:47 -0400354 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700355
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500356 m_mcastFaces[localEp] = face;
357 connectFaceClosedSignal(*face, [this, localEp] { m_mcastFaces.erase(localEp); });
Giulio Grassi624f6c62014-02-18 19:42:14 +0100358
Davide Pesavento292e5e12015-03-13 02:08:33 +0100359 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100360}
361
Davide Pesaventobb734df2017-10-24 18:05:36 -0400362static ndn::optional<ip::address>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500363pickAddress(const net::NetworkInterface& netif, net::AddressFamily af)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100364{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400365 for (const auto& na : netif.getNetworkAddresses()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500366 if (na.getFamily() == af &&
367 (na.getScope() == net::AddressScope::LINK || na.getScope() == net::AddressScope::GLOBAL)) {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400368 return na.getIp();
Junxiao Shibbace1d2017-08-06 20:03:37 +0000369 }
370 }
371 return ndn::nullopt;
372}
373
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500374std::vector<shared_ptr<Face>>
Davide Pesaventobb734df2017-10-24 18:05:36 -0400375UdpFactory::applyMcastConfigToNetif(const shared_ptr<const net::NetworkInterface>& netif)
Junxiao Shibbace1d2017-08-06 20:03:37 +0000376{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400377 BOOST_ASSERT(netif != nullptr);
378
Junxiao Shibbace1d2017-08-06 20:03:37 +0000379 if (!m_mcastConfig.isEnabled) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500380 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000381 }
382
383 if (!netif->isUp()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500384 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is down");
385 return {};
386 }
387
388 if (netif->isLoopback()) {
389 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is loopback");
390 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000391 }
392
393 if (!netif->canMulticast()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500394 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif cannot multicast");
395 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000396 }
397
398 if (!m_mcastConfig.netifPredicate(*netif)) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500399 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": rejected by whitelist/blacklist");
400 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000401 }
402
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500403 std::vector<ip::address> addrs;
404 for (auto af : {net::AddressFamily::V4, net::AddressFamily::V6}) {
405 auto addr = pickAddress(*netif, af);
406 if (addr)
407 addrs.push_back(*addr);
Junxiao Shibbace1d2017-08-06 20:03:37 +0000408 }
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500409
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500410 if (addrs.empty()) {
411 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": no viable IP address");
412 // keep an eye on new addresses
413 m_netifConns[netif->getIndex()].addrAddConn =
414 netif->onAddressAdded.connect(bind(&UdpFactory::applyMcastConfigToNetif, this, netif));
415 return {};
416 }
417
418 NFD_LOG_DEBUG("Creating multicast faces on " << netif->getName());
419
420 std::vector<shared_ptr<Face>> faces;
421 for (const auto& addr : addrs) {
422 auto face = this->createMulticastFace(netif, addr,
423 addr.is_v4() ? m_mcastConfig.group : m_mcastConfig.groupV6);
424 if (face->getId() == INVALID_FACEID) {
425 // new face: register with forwarding
426 this->addFace(face);
427 }
428 faces.push_back(std::move(face));
429 }
430
431 return faces;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000432}
433
Junxiao Shi64d99f22017-01-21 23:06:36 +0000434void
Junxiao Shibbace1d2017-08-06 20:03:37 +0000435UdpFactory::applyMcastConfig(const FaceSystem::ConfigContext& context)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000436{
437 // collect old faces
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500438 std::set<shared_ptr<Face>> facesToClose;
439 boost::copy(m_mcastFaces | boost::adaptors::map_values,
440 std::inserter(facesToClose, facesToClose.end()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000441
Junxiao Shibbace1d2017-08-06 20:03:37 +0000442 // create faces if requested by config
443 for (const auto& netif : netmon->listNetworkInterfaces()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500444 auto facesToKeep = this->applyMcastConfigToNetif(netif);
445 for (const auto& face : facesToKeep) {
Junxiao Shibbace1d2017-08-06 20:03:37 +0000446 // don't destroy face
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500447 facesToClose.erase(face);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000448 }
449 }
450
451 // destroy old faces that are not needed in new configuration
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500452 for (const auto& face : facesToClose) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000453 face->close();
454 }
455}
456
457} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100458} // namespace nfd