blob: e649871fab692293cac1ca10f6825a035d695de4 [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
Davide Pesaventobb734df2017-10-24 18:05:36 -040031#include <boost/range/adaptor/map.hpp>
Junxiao Shi64d99f22017-01-21 23:06:36 +000032#include <boost/range/algorithm/copy.hpp>
Giulio Grassi624f6c62014-02-18 19:42:14 +010033
34namespace nfd {
Junxiao Shi64d99f22017-01-21 23:06:36 +000035namespace face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010036
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020037namespace ip = boost::asio::ip;
Davide Pesaventobb734df2017-10-24 18:05:36 -040038namespace net = ndn::net;
Junxiao Shi79494162014-04-02 18:25:11 -070039
Davide Pesaventoa3148082018-04-12 18:21:54 -040040NFD_LOG_INIT(UdpFactory);
Junxiao Shib47247d2017-01-24 15:09:16 +000041NFD_REGISTER_PROTOCOL_FACTORY(UdpFactory);
42
43const std::string&
44UdpFactory::getId()
45{
46 static std::string id("udp");
47 return id;
48}
49
Junxiao Shi0ba6d642017-07-17 00:53:22 +000050UdpFactory::UdpFactory(const CtorParams& params)
51 : ProtocolFactory(params)
52{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040053 m_netifAddConn = netmon->onInterfaceAdded.connect([this] (const auto& netif) {
54 this->applyMcastConfigToNetif(netif);
55 });
Junxiao Shi0ba6d642017-07-17 00:53:22 +000056}
Giulio Grassi624f6c62014-02-18 19:42:14 +010057
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060058void
Junxiao Shi64d99f22017-01-21 23:06:36 +000059UdpFactory::processConfig(OptionalConfigSection configSection,
60 FaceSystem::ConfigContext& context)
61{
62 // udp
63 // {
Davide Pesavento494a9552018-02-04 22:16:05 -050064 // listen yes
Junxiao Shi64d99f22017-01-21 23:06:36 +000065 // port 6363
66 // enable_v4 yes
67 // enable_v6 yes
68 // idle_timeout 600
Junxiao Shi64d99f22017-01-21 23:06:36 +000069 // mcast yes
70 // mcast_group 224.0.23.170
71 // mcast_port 56363
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050072 // mcast_group_v6 ff02::1234
73 // mcast_port_v6 56363
Teng Liangfe4fce32017-03-29 04:49:38 +000074 // mcast_ad_hoc no
Junxiao Shic31080d2017-01-24 15:10:12 +000075 // whitelist
76 // {
77 // *
78 // }
79 // blacklist
80 // {
81 // }
Junxiao Shi64d99f22017-01-21 23:06:36 +000082 // }
83
Eric Newberry0c841642018-01-17 15:01:00 -070084 m_wantCongestionMarking = context.generalConfig.wantCongestionMarking;
85
Davide Pesavento494a9552018-02-04 22:16:05 -050086 bool wantListen = true;
Junxiao Shi64d99f22017-01-21 23:06:36 +000087 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
Davide Pesavento494a9552018-02-04 22:16:05 -0500101 if (key == "listen") {
102 wantListen = ConfigFile::parseYesNo(pair, "face_system.udp");
103 }
104 else if (key == "port") {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000105 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp");
106 }
107 else if (key == "enable_v4") {
108 enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp");
109 }
110 else if (key == "enable_v6") {
111 enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp");
112 }
113 else if (key == "idle_timeout") {
114 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp");
115 }
116 else if (key == "keep_alive_interval") {
117 // ignored
118 }
119 else if (key == "mcast") {
120 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp");
121 }
122 else if (key == "mcast_group") {
123 const std::string& valueStr = value.get_value<std::string>();
124 boost::system::error_code ec;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400125 mcastConfig.group.address(ip::address_v4::from_string(valueStr, ec));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000126 if (ec) {
127 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
128 valueStr + "' cannot be parsed as an IPv4 address"));
129 }
130 else if (!mcastConfig.group.address().is_multicast()) {
131 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
132 valueStr + "' is not a multicast address"));
133 }
134 }
135 else if (key == "mcast_port") {
136 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
137 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500138 else if (key == "mcast_group_v6") {
139 const std::string& valueStr = value.get_value<std::string>();
140 boost::system::error_code ec;
Davide Pesavento9c33b902018-05-20 01:30:29 -0400141 mcastConfig.groupV6.address(ip::address_v6::from_string(valueStr, ec));
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500142 if (ec) {
143 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
144 valueStr + "' cannot be parsed as an IPv6 address"));
145 }
146 else if (!mcastConfig.groupV6.address().is_multicast()) {
147 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
148 valueStr + "' is not a multicast address"));
149 }
150 }
151 else if (key == "mcast_port_v6") {
152 mcastConfig.groupV6.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
153 }
Teng Liangfe4fce32017-03-29 04:49:38 +0000154 else if (key == "mcast_ad_hoc") {
155 bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.udp");
156 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
157 }
Junxiao Shic31080d2017-01-24 15:10:12 +0000158 else if (key == "whitelist") {
159 mcastConfig.netifPredicate.parseWhitelist(value);
160 }
161 else if (key == "blacklist") {
162 mcastConfig.netifPredicate.parseBlacklist(value);
163 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000164 else {
165 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.udp." + key));
166 }
167 }
168
169 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
170 BOOST_THROW_EXCEPTION(ConfigFile::Error(
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."));
173 }
174 }
175
Junxiao Shi79a92082017-08-08 02:40:59 +0000176 if (context.isDryRun) {
177 return;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000178 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000179
180 if (enableV4) {
181 udp::Endpoint endpoint(ip::udp::v4(), port);
182 shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
Davide Pesavento494a9552018-02-04 22:16:05 -0500183 if (wantListen && !v4Channel->isListening()) {
Junxiao Shi79a92082017-08-08 02:40:59 +0000184 v4Channel->listen(this->addFace, nullptr);
185 }
186 providedSchemes.insert("udp");
187 providedSchemes.insert("udp4");
188 }
189 else if (providedSchemes.count("udp4") > 0) {
190 NFD_LOG_WARN("Cannot close udp4 channel after its creation");
191 }
192
193 if (enableV6) {
194 udp::Endpoint endpoint(ip::udp::v6(), port);
195 shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
Davide Pesavento494a9552018-02-04 22:16:05 -0500196 if (wantListen && !v6Channel->isListening()) {
Junxiao Shi79a92082017-08-08 02:40:59 +0000197 v6Channel->listen(this->addFace, nullptr);
198 }
199 providedSchemes.insert("udp");
200 providedSchemes.insert("udp6");
201 }
202 else if (providedSchemes.count("udp6") > 0) {
203 NFD_LOG_WARN("Cannot close udp6 channel after its creation");
204 }
205
206 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
207 if (mcastConfig.isEnabled) {
208 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500209 NFD_LOG_INFO("enabling multicast on " << mcastConfig.groupV6);
Junxiao Shi79a92082017-08-08 02:40:59 +0000210 }
211 else {
212 NFD_LOG_INFO("disabling multicast");
213 }
214 }
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");
218 }
219 if (m_mcastConfig.group != mcastConfig.group) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500220 NFD_LOG_INFO("changing IPv4 multicast group from " << m_mcastConfig.group <<
Junxiao Shi79a92082017-08-08 02:40:59 +0000221 " to " << mcastConfig.group);
222 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500223 if (m_mcastConfig.groupV6 != mcastConfig.groupV6) {
224 NFD_LOG_INFO("changing IPv6 multicast group from " << m_mcastConfig.groupV6 <<
225 " to " << mcastConfig.groupV6);
226 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000227 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
228 NFD_LOG_INFO("changing whitelist/blacklist");
229 }
230 }
231
232 // Even if there's no configuration change, we still need to re-apply configuration because
233 // netifs may have changed.
234 m_mcastConfig = mcastConfig;
235 this->applyMcastConfig(context);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000236}
237
238void
Davide Pesavento15b55052018-01-27 19:09:28 -0500239UdpFactory::createFace(const CreateFaceRequest& req,
Junxiao Shi64d99f22017-01-21 23:06:36 +0000240 const FaceCreatedCallback& onCreated,
241 const FaceCreationFailedCallback& onFailure)
242{
Davide Pesavento15b55052018-01-27 19:09:28 -0500243 BOOST_ASSERT(req.remoteUri.isCanonical());
Eric Newberry78e32b02017-04-01 14:34:44 +0000244
Davide Pesavento15b55052018-01-27 19:09:28 -0500245 if (req.localUri) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000246 NFD_LOG_TRACE("Cannot create unicast UDP face with LocalUri");
247 onFailure(406, "Unicast UDP faces cannot be created with a LocalUri");
248 return;
249 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000250
Davide Pesavento15b55052018-01-27 19:09:28 -0500251 if (req.params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000252 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
Davide Pesavento46afec42017-05-28 14:28:47 -0400253 onFailure(406, "Outgoing UDP faces do not support on-demand persistency");
Junxiao Shi64d99f22017-01-21 23:06:36 +0000254 return;
255 }
256
Davide Pesavento9c33b902018-05-20 01:30:29 -0400257 udp::Endpoint endpoint(ip::address::from_string(req.remoteUri.getHost()),
Davide Pesavento15b55052018-01-27 19:09:28 -0500258 boost::lexical_cast<uint16_t>(req.remoteUri.getPort()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000259
260 if (endpoint.address().is_multicast()) {
261 NFD_LOG_TRACE("createFace does not support multicast faces");
262 onFailure(406, "Cannot create multicast UDP faces");
263 return;
264 }
265
Davide Pesavento15b55052018-01-27 19:09:28 -0500266 if (req.params.wantLocalFields) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000267 // UDP faces are never local
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");
270 return;
271 }
272
Eric Newberry812d6152018-06-06 15:06:01 -0700273 if (req.params.mtu && *req.params.mtu < Transport::MIN_MTU) {
274 // The specified MTU must be greater than the minimum possible
275 NFD_LOG_TRACE("createFace cannot create a face with MTU less than " << Transport::MIN_MTU);
276 onFailure(406, "MTU cannot be less than " + to_string(Transport::MIN_MTU));
277 return;
278 }
279
Junxiao Shi64d99f22017-01-21 23:06:36 +0000280 // very simple logic for now
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())) {
Davide Pesavento15b55052018-01-27 19:09:28 -0500284 i.second->connect(endpoint, req.params, onCreated, onFailure);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000285 return;
286 }
287 }
288
Davide Pesavento46afec42017-05-28 14:28:47 -0400289 NFD_LOG_TRACE("No channels available to connect to " << endpoint);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000290 onFailure(504, "No channels available to connect");
291}
292
Giulio Grassi624f6c62014-02-18 19:42:14 +0100293shared_ptr<UdpChannel>
Davide Pesavento46afec42017-05-28 14:28:47 -0400294UdpFactory::createChannel(const udp::Endpoint& localEndpoint,
295 time::nanoseconds idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100296{
Davide Pesavento46afec42017-05-28 14:28:47 -0400297 auto it = m_channels.find(localEndpoint);
298 if (it != m_channels.end())
299 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100300
Yukai Tu0a49d342015-09-13 12:54:22 +0800301 // check if the endpoint is already used by a multicast face
Davide Pesavento46afec42017-05-28 14:28:47 -0400302 if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500303 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"));
Yukai Tu0a49d342015-09-13 12:54:22 +0800306 }
Junxiao Shi79494162014-04-02 18:25:11 -0700307
Eric Newberry0c841642018-01-17 15:01:00 -0700308 auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout, m_wantCongestionMarking);
Davide Pesavento46afec42017-05-28 14:28:47 -0400309 m_channels[localEndpoint] = channel;
Davide Pesavento46afec42017-05-28 14:28:47 -0400310
Giulio Grassi624f6c62014-02-18 19:42:14 +0100311 return channel;
312}
313
Junxiao Shi64d99f22017-01-21 23:06:36 +0000314std::vector<shared_ptr<const Channel>>
315UdpFactory::getChannels() const
316{
317 return getChannelsFromMap(m_channels);
318}
319
Junxiao Shicde37ad2015-12-24 01:02:05 -0700320shared_ptr<Face>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500321UdpFactory::createMulticastFace(const shared_ptr<const net::NetworkInterface>& netif,
322 const ip::address& localAddress,
323 const udp::Endpoint& multicastEndpoint)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100324{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400325 BOOST_ASSERT(multicastEndpoint.address().is_multicast());
Davide Pesaventobb734df2017-10-24 18:05:36 -0400326
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500327 udp::Endpoint localEp(localAddress, multicastEndpoint.port());
328 BOOST_ASSERT(localEp.protocol() == multicastEndpoint.protocol());
329
330 auto mcastEp = multicastEndpoint;
331 if (mcastEp.address().is_v6()) {
332 // in IPv6, a scope id on the multicast address is always required
333 auto mcastAddress = mcastEp.address().to_v6();
334 mcastAddress.scope_id(netif->getIndex());
335 mcastEp.address(mcastAddress);
336 }
337
338 // check if the local endpoint is already used by another multicast face
339 auto it = m_mcastFaces.find(localEp);
Davide Pesavento46afec42017-05-28 14:28:47 -0400340 if (it != m_mcastFaces.end()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500341 if (it->second->getRemoteUri() == FaceUri(mcastEp))
Davide Pesavento46afec42017-05-28 14:28:47 -0400342 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100343 else
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500344 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"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100347 }
Junxiao Shi79494162014-04-02 18:25:11 -0700348
Davide Pesaventobb734df2017-10-24 18:05:36 -0400349 // check if the local endpoint is already used by a unicast channel
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500350 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"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100354 }
Junxiao Shi79494162014-04-02 18:25:11 -0700355
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500356 ip::udp::socket rxSock(getGlobalIoService());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500357 MulticastUdpTransport::openRxSocket(rxSock, mcastEp, localAddress, netif);
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500358 ip::udp::socket txSock(getGlobalIoService());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500359 MulticastUdpTransport::openTxSocket(txSock, udp::Endpoint(localAddress, 0), netif);
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200360
Eric Newberry0c841642018-01-17 15:01:00 -0700361 GenericLinkService::Options options;
362 options.allowCongestionMarking = m_wantCongestionMarking;
363 auto linkService = make_unique<GenericLinkService>(options);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500364 auto transport = make_unique<MulticastUdpTransport>(mcastEp, std::move(rxSock), std::move(txSock),
Teng Liangfe4fce32017-03-29 04:49:38 +0000365 m_mcastConfig.linkType);
Davide Pesavento46afec42017-05-28 14:28:47 -0400366 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700367
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500368 m_mcastFaces[localEp] = face;
369 connectFaceClosedSignal(*face, [this, localEp] { m_mcastFaces.erase(localEp); });
Giulio Grassi624f6c62014-02-18 19:42:14 +0100370
Davide Pesavento292e5e12015-03-13 02:08:33 +0100371 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100372}
373
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400374static optional<ip::address>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500375pickAddress(const net::NetworkInterface& netif, net::AddressFamily af)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100376{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400377 for (const auto& na : netif.getNetworkAddresses()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500378 if (na.getFamily() == af &&
379 (na.getScope() == net::AddressScope::LINK || na.getScope() == net::AddressScope::GLOBAL)) {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400380 return na.getIp();
Junxiao Shibbace1d2017-08-06 20:03:37 +0000381 }
382 }
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400383 return nullopt;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000384}
385
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500386std::vector<shared_ptr<Face>>
Davide Pesaventobb734df2017-10-24 18:05:36 -0400387UdpFactory::applyMcastConfigToNetif(const shared_ptr<const net::NetworkInterface>& netif)
Junxiao Shibbace1d2017-08-06 20:03:37 +0000388{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400389 BOOST_ASSERT(netif != nullptr);
390
Junxiao Shibbace1d2017-08-06 20:03:37 +0000391 if (!m_mcastConfig.isEnabled) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500392 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000393 }
394
395 if (!netif->isUp()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500396 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is down");
397 return {};
398 }
399
400 if (netif->isLoopback()) {
401 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is loopback");
402 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000403 }
404
405 if (!netif->canMulticast()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500406 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif cannot multicast");
407 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000408 }
409
410 if (!m_mcastConfig.netifPredicate(*netif)) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500411 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": rejected by whitelist/blacklist");
412 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000413 }
414
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500415 std::vector<ip::address> addrs;
416 for (auto af : {net::AddressFamily::V4, net::AddressFamily::V6}) {
417 auto addr = pickAddress(*netif, af);
418 if (addr)
419 addrs.push_back(*addr);
Junxiao Shibbace1d2017-08-06 20:03:37 +0000420 }
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500421
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500422 if (addrs.empty()) {
423 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": no viable IP address");
424 // keep an eye on new addresses
425 m_netifConns[netif->getIndex()].addrAddConn =
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400426 netif->onAddressAdded.connect([=] (auto...) { this->applyMcastConfigToNetif(netif); });
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500427 return {};
428 }
429
430 NFD_LOG_DEBUG("Creating multicast faces on " << netif->getName());
431
432 std::vector<shared_ptr<Face>> faces;
433 for (const auto& addr : addrs) {
434 auto face = this->createMulticastFace(netif, addr,
435 addr.is_v4() ? m_mcastConfig.group : m_mcastConfig.groupV6);
436 if (face->getId() == INVALID_FACEID) {
437 // new face: register with forwarding
438 this->addFace(face);
439 }
440 faces.push_back(std::move(face));
441 }
442
443 return faces;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000444}
445
Junxiao Shi64d99f22017-01-21 23:06:36 +0000446void
Junxiao Shibbace1d2017-08-06 20:03:37 +0000447UdpFactory::applyMcastConfig(const FaceSystem::ConfigContext& context)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000448{
449 // collect old faces
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500450 std::set<shared_ptr<Face>> facesToClose;
451 boost::copy(m_mcastFaces | boost::adaptors::map_values,
452 std::inserter(facesToClose, facesToClose.end()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000453
Junxiao Shibbace1d2017-08-06 20:03:37 +0000454 // create faces if requested by config
455 for (const auto& netif : netmon->listNetworkInterfaces()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500456 auto facesToKeep = this->applyMcastConfigToNetif(netif);
457 for (const auto& face : facesToKeep) {
Junxiao Shibbace1d2017-08-06 20:03:37 +0000458 // don't destroy face
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500459 facesToClose.erase(face);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000460 }
461 }
462
463 // destroy old faces that are not needed in new configuration
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500464 for (const auto& face : facesToClose) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000465 face->close();
466 }
467}
468
469} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100470} // namespace nfd