blob: 2b7b3c81dccf74bb52ec60b9f797eb306fae0822 [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
Davide Pesaventoa3148082018-04-12 18:21:54 -040041NFD_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 // {
Davide Pesavento494a9552018-02-04 22:16:05 -050063 // listen yes
Junxiao Shi64d99f22017-01-21 23:06:36 +000064 // port 6363
65 // enable_v4 yes
66 // enable_v6 yes
67 // idle_timeout 600
Junxiao Shi64d99f22017-01-21 23:06:36 +000068 // 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
Davide Pesavento494a9552018-02-04 22:16:05 -050085 bool wantListen = true;
Junxiao Shi64d99f22017-01-21 23:06:36 +000086 uint16_t port = 6363;
87 bool enableV4 = false;
88 bool enableV6 = false;
89 uint32_t idleTimeout = 600;
90 MulticastConfig mcastConfig;
91
92 if (configSection) {
93 // These default to 'yes' but only if face_system.udp section is present
94 enableV4 = enableV6 = mcastConfig.isEnabled = true;
95
96 for (const auto& pair : *configSection) {
97 const std::string& key = pair.first;
98 const ConfigSection& value = pair.second;
99
Davide Pesavento494a9552018-02-04 22:16:05 -0500100 if (key == "listen") {
101 wantListen = ConfigFile::parseYesNo(pair, "face_system.udp");
102 }
103 else if (key == "port") {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000104 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp");
105 }
106 else if (key == "enable_v4") {
107 enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp");
108 }
109 else if (key == "enable_v6") {
110 enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp");
111 }
112 else if (key == "idle_timeout") {
113 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp");
114 }
115 else if (key == "keep_alive_interval") {
116 // ignored
117 }
118 else if (key == "mcast") {
119 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp");
120 }
121 else if (key == "mcast_group") {
122 const std::string& valueStr = value.get_value<std::string>();
123 boost::system::error_code ec;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400124 mcastConfig.group.address(ip::address_v4::from_string(valueStr, ec));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000125 if (ec) {
126 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
127 valueStr + "' cannot be parsed as an IPv4 address"));
128 }
129 else if (!mcastConfig.group.address().is_multicast()) {
130 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
131 valueStr + "' is not a multicast address"));
132 }
133 }
134 else if (key == "mcast_port") {
135 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
136 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500137 else if (key == "mcast_group_v6") {
138 const std::string& valueStr = value.get_value<std::string>();
139 boost::system::error_code ec;
140 mcastConfig.groupV6.address(ndn::ip::addressV6FromString(valueStr, ec));
141 if (ec) {
142 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
143 valueStr + "' cannot be parsed as an IPv6 address"));
144 }
145 else if (!mcastConfig.groupV6.address().is_multicast()) {
146 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
147 valueStr + "' is not a multicast address"));
148 }
149 }
150 else if (key == "mcast_port_v6") {
151 mcastConfig.groupV6.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
152 }
Teng Liangfe4fce32017-03-29 04:49:38 +0000153 else if (key == "mcast_ad_hoc") {
154 bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.udp");
155 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
156 }
Junxiao Shic31080d2017-01-24 15:10:12 +0000157 else if (key == "whitelist") {
158 mcastConfig.netifPredicate.parseWhitelist(value);
159 }
160 else if (key == "blacklist") {
161 mcastConfig.netifPredicate.parseBlacklist(value);
162 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000163 else {
164 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.udp." + key));
165 }
166 }
167
168 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
169 BOOST_THROW_EXCEPTION(ConfigFile::Error(
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."));
172 }
173 }
174
Junxiao Shi79a92082017-08-08 02:40:59 +0000175 if (context.isDryRun) {
176 return;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000177 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000178
179 if (enableV4) {
180 udp::Endpoint endpoint(ip::udp::v4(), port);
181 shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
Davide Pesavento494a9552018-02-04 22:16:05 -0500182 if (wantListen && !v4Channel->isListening()) {
Junxiao Shi79a92082017-08-08 02:40:59 +0000183 v4Channel->listen(this->addFace, nullptr);
184 }
185 providedSchemes.insert("udp");
186 providedSchemes.insert("udp4");
187 }
188 else if (providedSchemes.count("udp4") > 0) {
189 NFD_LOG_WARN("Cannot close udp4 channel after its creation");
190 }
191
192 if (enableV6) {
193 udp::Endpoint endpoint(ip::udp::v6(), port);
194 shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
Davide Pesavento494a9552018-02-04 22:16:05 -0500195 if (wantListen && !v6Channel->isListening()) {
Junxiao Shi79a92082017-08-08 02:40:59 +0000196 v6Channel->listen(this->addFace, nullptr);
197 }
198 providedSchemes.insert("udp");
199 providedSchemes.insert("udp6");
200 }
201 else if (providedSchemes.count("udp6") > 0) {
202 NFD_LOG_WARN("Cannot close udp6 channel after its creation");
203 }
204
205 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
206 if (mcastConfig.isEnabled) {
207 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500208 NFD_LOG_INFO("enabling multicast on " << mcastConfig.groupV6);
Junxiao Shi79a92082017-08-08 02:40:59 +0000209 }
210 else {
211 NFD_LOG_INFO("disabling multicast");
212 }
213 }
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");
217 }
218 if (m_mcastConfig.group != mcastConfig.group) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500219 NFD_LOG_INFO("changing IPv4 multicast group from " << m_mcastConfig.group <<
Junxiao Shi79a92082017-08-08 02:40:59 +0000220 " to " << mcastConfig.group);
221 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500222 if (m_mcastConfig.groupV6 != mcastConfig.groupV6) {
223 NFD_LOG_INFO("changing IPv6 multicast group from " << m_mcastConfig.groupV6 <<
224 " to " << mcastConfig.groupV6);
225 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000226 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
227 NFD_LOG_INFO("changing whitelist/blacklist");
228 }
229 }
230
231 // Even if there's no configuration change, we still need to re-apply configuration because
232 // netifs may have changed.
233 m_mcastConfig = mcastConfig;
234 this->applyMcastConfig(context);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000235}
236
237void
Davide Pesavento15b55052018-01-27 19:09:28 -0500238UdpFactory::createFace(const CreateFaceRequest& req,
Junxiao Shi64d99f22017-01-21 23:06:36 +0000239 const FaceCreatedCallback& onCreated,
240 const FaceCreationFailedCallback& onFailure)
241{
Davide Pesavento15b55052018-01-27 19:09:28 -0500242 BOOST_ASSERT(req.remoteUri.isCanonical());
Eric Newberry78e32b02017-04-01 14:34:44 +0000243
Davide Pesavento15b55052018-01-27 19:09:28 -0500244 if (req.localUri) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000245 NFD_LOG_TRACE("Cannot create unicast UDP face with LocalUri");
246 onFailure(406, "Unicast UDP faces cannot be created with a LocalUri");
247 return;
248 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000249
Davide Pesavento15b55052018-01-27 19:09:28 -0500250 if (req.params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000251 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
Davide Pesavento46afec42017-05-28 14:28:47 -0400252 onFailure(406, "Outgoing UDP faces do not support on-demand persistency");
Junxiao Shi64d99f22017-01-21 23:06:36 +0000253 return;
254 }
255
Davide Pesavento15b55052018-01-27 19:09:28 -0500256 udp::Endpoint endpoint(ndn::ip::addressFromString(req.remoteUri.getHost()),
257 boost::lexical_cast<uint16_t>(req.remoteUri.getPort()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000258
259 if (endpoint.address().is_multicast()) {
260 NFD_LOG_TRACE("createFace does not support multicast faces");
261 onFailure(406, "Cannot create multicast UDP faces");
262 return;
263 }
264
Davide Pesavento15b55052018-01-27 19:09:28 -0500265 if (req.params.wantLocalFields) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000266 // UDP faces are never local
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");
269 return;
270 }
271
272 // very simple logic for now
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())) {
Davide Pesavento15b55052018-01-27 19:09:28 -0500276 i.second->connect(endpoint, req.params, onCreated, onFailure);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000277 return;
278 }
279 }
280
Davide Pesavento46afec42017-05-28 14:28:47 -0400281 NFD_LOG_TRACE("No channels available to connect to " << endpoint);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000282 onFailure(504, "No channels available to connect");
283}
284
Giulio Grassi624f6c62014-02-18 19:42:14 +0100285shared_ptr<UdpChannel>
Davide Pesavento46afec42017-05-28 14:28:47 -0400286UdpFactory::createChannel(const udp::Endpoint& localEndpoint,
287 time::nanoseconds idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100288{
Davide Pesavento46afec42017-05-28 14:28:47 -0400289 auto it = m_channels.find(localEndpoint);
290 if (it != m_channels.end())
291 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100292
Yukai Tu0a49d342015-09-13 12:54:22 +0800293 // check if the endpoint is already used by a multicast face
Davide Pesavento46afec42017-05-28 14:28:47 -0400294 if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500295 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"));
Yukai Tu0a49d342015-09-13 12:54:22 +0800298 }
Junxiao Shi79494162014-04-02 18:25:11 -0700299
Eric Newberry0c841642018-01-17 15:01:00 -0700300 auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout, m_wantCongestionMarking);
Davide Pesavento46afec42017-05-28 14:28:47 -0400301 m_channels[localEndpoint] = channel;
Davide Pesavento46afec42017-05-28 14:28:47 -0400302
Giulio Grassi624f6c62014-02-18 19:42:14 +0100303 return channel;
304}
305
Junxiao Shi64d99f22017-01-21 23:06:36 +0000306std::vector<shared_ptr<const Channel>>
307UdpFactory::getChannels() const
308{
309 return getChannelsFromMap(m_channels);
310}
311
Junxiao Shicde37ad2015-12-24 01:02:05 -0700312shared_ptr<Face>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500313UdpFactory::createMulticastFace(const shared_ptr<const net::NetworkInterface>& netif,
314 const ip::address& localAddress,
315 const udp::Endpoint& multicastEndpoint)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100316{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400317 BOOST_ASSERT(multicastEndpoint.address().is_multicast());
Davide Pesaventobb734df2017-10-24 18:05:36 -0400318
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500319 udp::Endpoint localEp(localAddress, multicastEndpoint.port());
320 BOOST_ASSERT(localEp.protocol() == multicastEndpoint.protocol());
321
322 auto mcastEp = multicastEndpoint;
323 if (mcastEp.address().is_v6()) {
324 // in IPv6, a scope id on the multicast address is always required
325 auto mcastAddress = mcastEp.address().to_v6();
326 mcastAddress.scope_id(netif->getIndex());
327 mcastEp.address(mcastAddress);
328 }
329
330 // check if the local endpoint is already used by another multicast face
331 auto it = m_mcastFaces.find(localEp);
Davide Pesavento46afec42017-05-28 14:28:47 -0400332 if (it != m_mcastFaces.end()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500333 if (it->second->getRemoteUri() == FaceUri(mcastEp))
Davide Pesavento46afec42017-05-28 14:28:47 -0400334 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100335 else
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500336 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"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100339 }
Junxiao Shi79494162014-04-02 18:25:11 -0700340
Davide Pesaventobb734df2017-10-24 18:05:36 -0400341 // check if the local endpoint is already used by a unicast channel
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500342 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"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100346 }
Junxiao Shi79494162014-04-02 18:25:11 -0700347
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500348 ip::udp::socket rxSock(getGlobalIoService());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500349 MulticastUdpTransport::openRxSocket(rxSock, mcastEp, localAddress, netif);
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500350 ip::udp::socket txSock(getGlobalIoService());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500351 MulticastUdpTransport::openTxSocket(txSock, udp::Endpoint(localAddress, 0), netif);
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200352
Eric Newberry0c841642018-01-17 15:01:00 -0700353 GenericLinkService::Options options;
354 options.allowCongestionMarking = m_wantCongestionMarking;
355 auto linkService = make_unique<GenericLinkService>(options);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500356 auto transport = make_unique<MulticastUdpTransport>(mcastEp, std::move(rxSock), std::move(txSock),
Teng Liangfe4fce32017-03-29 04:49:38 +0000357 m_mcastConfig.linkType);
Davide Pesavento46afec42017-05-28 14:28:47 -0400358 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700359
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500360 m_mcastFaces[localEp] = face;
361 connectFaceClosedSignal(*face, [this, localEp] { m_mcastFaces.erase(localEp); });
Giulio Grassi624f6c62014-02-18 19:42:14 +0100362
Davide Pesavento292e5e12015-03-13 02:08:33 +0100363 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100364}
365
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400366static optional<ip::address>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500367pickAddress(const net::NetworkInterface& netif, net::AddressFamily af)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100368{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400369 for (const auto& na : netif.getNetworkAddresses()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500370 if (na.getFamily() == af &&
371 (na.getScope() == net::AddressScope::LINK || na.getScope() == net::AddressScope::GLOBAL)) {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400372 return na.getIp();
Junxiao Shibbace1d2017-08-06 20:03:37 +0000373 }
374 }
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400375 return nullopt;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000376}
377
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500378std::vector<shared_ptr<Face>>
Davide Pesaventobb734df2017-10-24 18:05:36 -0400379UdpFactory::applyMcastConfigToNetif(const shared_ptr<const net::NetworkInterface>& netif)
Junxiao Shibbace1d2017-08-06 20:03:37 +0000380{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400381 BOOST_ASSERT(netif != nullptr);
382
Junxiao Shibbace1d2017-08-06 20:03:37 +0000383 if (!m_mcastConfig.isEnabled) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500384 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000385 }
386
387 if (!netif->isUp()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500388 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is down");
389 return {};
390 }
391
392 if (netif->isLoopback()) {
393 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is loopback");
394 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000395 }
396
397 if (!netif->canMulticast()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500398 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif cannot multicast");
399 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000400 }
401
402 if (!m_mcastConfig.netifPredicate(*netif)) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500403 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": rejected by whitelist/blacklist");
404 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000405 }
406
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500407 std::vector<ip::address> addrs;
408 for (auto af : {net::AddressFamily::V4, net::AddressFamily::V6}) {
409 auto addr = pickAddress(*netif, af);
410 if (addr)
411 addrs.push_back(*addr);
Junxiao Shibbace1d2017-08-06 20:03:37 +0000412 }
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500413
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500414 if (addrs.empty()) {
415 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": no viable IP address");
416 // keep an eye on new addresses
417 m_netifConns[netif->getIndex()].addrAddConn =
418 netif->onAddressAdded.connect(bind(&UdpFactory::applyMcastConfigToNetif, this, netif));
419 return {};
420 }
421
422 NFD_LOG_DEBUG("Creating multicast faces on " << netif->getName());
423
424 std::vector<shared_ptr<Face>> faces;
425 for (const auto& addr : addrs) {
426 auto face = this->createMulticastFace(netif, addr,
427 addr.is_v4() ? m_mcastConfig.group : m_mcastConfig.groupV6);
428 if (face->getId() == INVALID_FACEID) {
429 // new face: register with forwarding
430 this->addFace(face);
431 }
432 faces.push_back(std::move(face));
433 }
434
435 return faces;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000436}
437
Junxiao Shi64d99f22017-01-21 23:06:36 +0000438void
Junxiao Shibbace1d2017-08-06 20:03:37 +0000439UdpFactory::applyMcastConfig(const FaceSystem::ConfigContext& context)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000440{
441 // collect old faces
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500442 std::set<shared_ptr<Face>> facesToClose;
443 boost::copy(m_mcastFaces | boost::adaptors::map_values,
444 std::inserter(facesToClose, facesToClose.end()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000445
Junxiao Shibbace1d2017-08-06 20:03:37 +0000446 // create faces if requested by config
447 for (const auto& netif : netmon->listNetworkInterfaces()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500448 auto facesToKeep = this->applyMcastConfigToNetif(netif);
449 for (const auto& face : facesToKeep) {
Junxiao Shibbace1d2017-08-06 20:03:37 +0000450 // don't destroy face
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500451 facesToClose.erase(face);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000452 }
453 }
454
455 // destroy old faces that are not needed in new configuration
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500456 for (const auto& face : facesToClose) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000457 face->close();
458 }
459}
460
461} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100462} // namespace nfd