blob: 331174db8524bf679bc0d591960aaf6026ae57c5 [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/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, 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"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040029#include "common/global.hpp"
Yanbiao Liafb20592017-08-03 16:20:46 -070030
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040031#include <boost/lexical_cast.hpp>
Davide Pesaventobb734df2017-10-24 18:05:36 -040032#include <boost/range/adaptor/map.hpp>
Junxiao Shi64d99f22017-01-21 23:06:36 +000033#include <boost/range/algorithm/copy.hpp>
Giulio Grassi624f6c62014-02-18 19:42:14 +010034
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::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&
Davide Pesaventod27841b2018-11-13 00:22:24 -050044UdpFactory::getId() noexcept
Junxiao Shib47247d2017-01-24 15:09:16 +000045{
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
Davide Pesaventod27841b2018-11-13 00:22:24 -050059UdpFactory::doProcessConfig(OptionalConfigSection configSection,
60 FaceSystem::ConfigContext& context)
Junxiao Shi64d99f22017-01-21 23:06:36 +000061{
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 Shia6286a92021-02-23 06:43:52 -070069 // unicast_mtu 8800
Junxiao Shi64d99f22017-01-21 23:06:36 +000070 // mcast yes
71 // mcast_group 224.0.23.170
72 // mcast_port 56363
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050073 // mcast_group_v6 ff02::1234
74 // mcast_port_v6 56363
Teng Liangfe4fce32017-03-29 04:49:38 +000075 // mcast_ad_hoc no
Junxiao Shic31080d2017-01-24 15:10:12 +000076 // whitelist
77 // {
78 // *
79 // }
80 // blacklist
81 // {
82 // }
Junxiao Shi64d99f22017-01-21 23:06:36 +000083 // }
84
Eric Newberry0c841642018-01-17 15:01:00 -070085 m_wantCongestionMarking = context.generalConfig.wantCongestionMarking;
86
Davide Pesavento494a9552018-02-04 22:16:05 -050087 bool wantListen = true;
Junxiao Shi64d99f22017-01-21 23:06:36 +000088 uint16_t port = 6363;
89 bool enableV4 = false;
90 bool enableV6 = false;
91 uint32_t idleTimeout = 600;
Junxiao Shia6286a92021-02-23 06:43:52 -070092 size_t unicastMtu = ndn::MAX_NDN_PACKET_SIZE;
Junxiao Shi64d99f22017-01-21 23:06:36 +000093 MulticastConfig mcastConfig;
94
95 if (configSection) {
96 // These default to 'yes' but only if face_system.udp section is present
97 enableV4 = enableV6 = mcastConfig.isEnabled = true;
98
99 for (const auto& pair : *configSection) {
100 const std::string& key = pair.first;
101 const ConfigSection& value = pair.second;
102
Davide Pesavento494a9552018-02-04 22:16:05 -0500103 if (key == "listen") {
104 wantListen = ConfigFile::parseYesNo(pair, "face_system.udp");
105 }
106 else if (key == "port") {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000107 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp");
108 }
109 else if (key == "enable_v4") {
110 enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp");
111 }
112 else if (key == "enable_v6") {
113 enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp");
114 }
115 else if (key == "idle_timeout") {
116 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp");
117 }
Junxiao Shia6286a92021-02-23 06:43:52 -0700118 else if (key == "unicast_mtu") {
119 unicastMtu = ConfigFile::parseNumber<size_t>(pair, "face_system.udp");
120 ConfigFile::checkRange(unicastMtu, static_cast<size_t>(MIN_MTU), ndn::MAX_NDN_PACKET_SIZE,
121 "unicast_mtu", "face_system.udp");
122 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000123 else if (key == "keep_alive_interval") {
124 // ignored
125 }
126 else if (key == "mcast") {
127 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp");
128 }
129 else if (key == "mcast_group") {
130 const std::string& valueStr = value.get_value<std::string>();
131 boost::system::error_code ec;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400132 mcastConfig.group.address(ip::address_v4::from_string(valueStr, ec));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000133 if (ec) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500134 NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group: '" +
135 valueStr + "' cannot be parsed as an IPv4 address"));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000136 }
137 else if (!mcastConfig.group.address().is_multicast()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500138 NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group: '" +
139 valueStr + "' is not a multicast address"));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000140 }
141 }
142 else if (key == "mcast_port") {
143 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
144 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500145 else if (key == "mcast_group_v6") {
146 const std::string& valueStr = value.get_value<std::string>();
147 boost::system::error_code ec;
Davide Pesavento9c33b902018-05-20 01:30:29 -0400148 mcastConfig.groupV6.address(ip::address_v6::from_string(valueStr, ec));
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500149 if (ec) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500150 NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
151 valueStr + "' cannot be parsed as an IPv6 address"));
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500152 }
153 else if (!mcastConfig.groupV6.address().is_multicast()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500154 NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
155 valueStr + "' is not a multicast address"));
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500156 }
157 }
158 else if (key == "mcast_port_v6") {
159 mcastConfig.groupV6.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
160 }
Teng Liangfe4fce32017-03-29 04:49:38 +0000161 else if (key == "mcast_ad_hoc") {
162 bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.udp");
163 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
164 }
Junxiao Shic31080d2017-01-24 15:10:12 +0000165 else if (key == "whitelist") {
166 mcastConfig.netifPredicate.parseWhitelist(value);
167 }
168 else if (key == "blacklist") {
169 mcastConfig.netifPredicate.parseBlacklist(value);
170 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000171 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500172 NDN_THROW(ConfigFile::Error("Unrecognized option face_system.udp." + key));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000173 }
174 }
175
176 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500177 NDN_THROW(ConfigFile::Error(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000178 "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. "
179 "Remove face_system.udp section to disable UDP channels or enable at least one of them."));
180 }
181 }
182
Junxiao Shi79a92082017-08-08 02:40:59 +0000183 if (context.isDryRun) {
184 return;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000185 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000186
Junxiao Shia6286a92021-02-23 06:43:52 -0700187 m_defaultUnicastMtu = unicastMtu;
188
Junxiao Shi79a92082017-08-08 02:40:59 +0000189 if (enableV4) {
190 udp::Endpoint endpoint(ip::udp::v4(), port);
191 shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
Davide Pesavento494a9552018-02-04 22:16:05 -0500192 if (wantListen && !v4Channel->isListening()) {
Junxiao Shi79a92082017-08-08 02:40:59 +0000193 v4Channel->listen(this->addFace, nullptr);
194 }
195 providedSchemes.insert("udp");
196 providedSchemes.insert("udp4");
197 }
198 else if (providedSchemes.count("udp4") > 0) {
199 NFD_LOG_WARN("Cannot close udp4 channel after its creation");
200 }
201
202 if (enableV6) {
203 udp::Endpoint endpoint(ip::udp::v6(), port);
204 shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
Davide Pesavento494a9552018-02-04 22:16:05 -0500205 if (wantListen && !v6Channel->isListening()) {
Junxiao Shi79a92082017-08-08 02:40:59 +0000206 v6Channel->listen(this->addFace, nullptr);
207 }
208 providedSchemes.insert("udp");
209 providedSchemes.insert("udp6");
210 }
211 else if (providedSchemes.count("udp6") > 0) {
212 NFD_LOG_WARN("Cannot close udp6 channel after its creation");
213 }
214
215 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
216 if (mcastConfig.isEnabled) {
217 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500218 NFD_LOG_INFO("enabling multicast on " << mcastConfig.groupV6);
Junxiao Shi79a92082017-08-08 02:40:59 +0000219 }
220 else {
221 NFD_LOG_INFO("disabling multicast");
222 }
223 }
224 else if (mcastConfig.isEnabled) {
225 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
226 NFD_LOG_WARN("Cannot change ad hoc setting on existing faces");
227 }
228 if (m_mcastConfig.group != mcastConfig.group) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500229 NFD_LOG_INFO("changing IPv4 multicast group from " << m_mcastConfig.group <<
Junxiao Shi79a92082017-08-08 02:40:59 +0000230 " to " << mcastConfig.group);
231 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500232 if (m_mcastConfig.groupV6 != mcastConfig.groupV6) {
233 NFD_LOG_INFO("changing IPv6 multicast group from " << m_mcastConfig.groupV6 <<
234 " to " << mcastConfig.groupV6);
235 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000236 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
237 NFD_LOG_INFO("changing whitelist/blacklist");
238 }
239 }
240
241 // Even if there's no configuration change, we still need to re-apply configuration because
242 // netifs may have changed.
243 m_mcastConfig = mcastConfig;
244 this->applyMcastConfig(context);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000245}
246
247void
Davide Pesaventod27841b2018-11-13 00:22:24 -0500248UdpFactory::doCreateFace(const CreateFaceRequest& req,
249 const FaceCreatedCallback& onCreated,
250 const FaceCreationFailedCallback& onFailure)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000251{
Davide Pesavento15b55052018-01-27 19:09:28 -0500252 if (req.localUri) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000253 NFD_LOG_TRACE("Cannot create unicast UDP face with LocalUri");
254 onFailure(406, "Unicast UDP faces cannot be created with a LocalUri");
255 return;
256 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000257
Davide Pesavento15b55052018-01-27 19:09:28 -0500258 if (req.params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000259 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
Davide Pesavento46afec42017-05-28 14:28:47 -0400260 onFailure(406, "Outgoing UDP faces do not support on-demand persistency");
Junxiao Shi64d99f22017-01-21 23:06:36 +0000261 return;
262 }
263
Davide Pesavento9c33b902018-05-20 01:30:29 -0400264 udp::Endpoint endpoint(ip::address::from_string(req.remoteUri.getHost()),
Davide Pesavento15b55052018-01-27 19:09:28 -0500265 boost::lexical_cast<uint16_t>(req.remoteUri.getPort()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000266
267 if (endpoint.address().is_multicast()) {
268 NFD_LOG_TRACE("createFace does not support multicast faces");
269 onFailure(406, "Cannot create multicast UDP faces");
270 return;
271 }
272
Davide Pesavento15b55052018-01-27 19:09:28 -0500273 if (req.params.wantLocalFields) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000274 // UDP faces are never local
275 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
276 onFailure(406, "Local fields can only be enabled on faces with local scope");
277 return;
278 }
279
Eric Newberrycb6551e2020-03-02 14:12:16 -0800280 if (req.params.mtu && *req.params.mtu < MIN_MTU) {
Eric Newberry812d6152018-06-06 15:06:01 -0700281 // The specified MTU must be greater than the minimum possible
Eric Newberrycb6551e2020-03-02 14:12:16 -0800282 NFD_LOG_TRACE("createFace: override MTU cannot be less than " << MIN_MTU);
283 onFailure(406, "Override MTU cannot be less than " + to_string(MIN_MTU));
Eric Newberry812d6152018-06-06 15:06:01 -0700284 return;
285 }
286
Junxiao Shi64d99f22017-01-21 23:06:36 +0000287 // very simple logic for now
288 for (const auto& i : m_channels) {
289 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
290 (i.first.address().is_v6() && endpoint.address().is_v6())) {
Davide Pesavento15b55052018-01-27 19:09:28 -0500291 i.second->connect(endpoint, req.params, onCreated, onFailure);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000292 return;
293 }
294 }
295
Davide Pesavento46afec42017-05-28 14:28:47 -0400296 NFD_LOG_TRACE("No channels available to connect to " << endpoint);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000297 onFailure(504, "No channels available to connect");
298}
299
Giulio Grassi624f6c62014-02-18 19:42:14 +0100300shared_ptr<UdpChannel>
Davide Pesavento46afec42017-05-28 14:28:47 -0400301UdpFactory::createChannel(const udp::Endpoint& localEndpoint,
302 time::nanoseconds idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100303{
Davide Pesavento46afec42017-05-28 14:28:47 -0400304 auto it = m_channels.find(localEndpoint);
305 if (it != m_channels.end())
306 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100307
Yukai Tu0a49d342015-09-13 12:54:22 +0800308 // check if the endpoint is already used by a multicast face
Davide Pesavento46afec42017-05-28 14:28:47 -0400309 if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500310 NDN_THROW(Error("Cannot create UDP channel on " + boost::lexical_cast<std::string>(localEndpoint) +
311 ", endpoint already allocated to a UDP multicast face"));
Yukai Tu0a49d342015-09-13 12:54:22 +0800312 }
Junxiao Shi79494162014-04-02 18:25:11 -0700313
Junxiao Shia6286a92021-02-23 06:43:52 -0700314 auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout,
315 m_wantCongestionMarking, m_defaultUnicastMtu);
Davide Pesavento46afec42017-05-28 14:28:47 -0400316 m_channels[localEndpoint] = channel;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100317 return channel;
318}
319
Junxiao Shi64d99f22017-01-21 23:06:36 +0000320std::vector<shared_ptr<const Channel>>
Davide Pesaventod27841b2018-11-13 00:22:24 -0500321UdpFactory::doGetChannels() const
Junxiao Shi64d99f22017-01-21 23:06:36 +0000322{
323 return getChannelsFromMap(m_channels);
324}
325
Junxiao Shicde37ad2015-12-24 01:02:05 -0700326shared_ptr<Face>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500327UdpFactory::createMulticastFace(const shared_ptr<const net::NetworkInterface>& netif,
328 const ip::address& localAddress,
329 const udp::Endpoint& multicastEndpoint)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100330{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400331 BOOST_ASSERT(multicastEndpoint.address().is_multicast());
Davide Pesaventobb734df2017-10-24 18:05:36 -0400332
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500333 udp::Endpoint localEp(localAddress, multicastEndpoint.port());
334 BOOST_ASSERT(localEp.protocol() == multicastEndpoint.protocol());
335
336 auto mcastEp = multicastEndpoint;
337 if (mcastEp.address().is_v6()) {
338 // in IPv6, a scope id on the multicast address is always required
339 auto mcastAddress = mcastEp.address().to_v6();
340 mcastAddress.scope_id(netif->getIndex());
341 mcastEp.address(mcastAddress);
342 }
343
344 // check if the local endpoint is already used by another multicast face
345 auto it = m_mcastFaces.find(localEp);
Davide Pesavento46afec42017-05-28 14:28:47 -0400346 if (it != m_mcastFaces.end()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500347 if (it->second->getRemoteUri() == FaceUri(mcastEp))
Davide Pesavento46afec42017-05-28 14:28:47 -0400348 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100349 else
Davide Pesavento19779d82019-02-14 13:40:04 -0500350 NDN_THROW(Error("Cannot create UDP multicast face on " + boost::lexical_cast<std::string>(localEp) +
351 ", endpoint already allocated to a different UDP multicast face"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100352 }
Junxiao Shi79494162014-04-02 18:25:11 -0700353
Davide Pesaventobb734df2017-10-24 18:05:36 -0400354 // check if the local endpoint is already used by a unicast channel
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500355 if (m_channels.find(localEp) != m_channels.end()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500356 NDN_THROW(Error("Cannot create UDP multicast face on " + boost::lexical_cast<std::string>(localEp) +
357 ", endpoint already allocated to a UDP channel"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100358 }
Junxiao Shi79494162014-04-02 18:25:11 -0700359
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500360 ip::udp::socket rxSock(getGlobalIoService());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500361 MulticastUdpTransport::openRxSocket(rxSock, mcastEp, localAddress, netif);
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500362 ip::udp::socket txSock(getGlobalIoService());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500363 MulticastUdpTransport::openTxSocket(txSock, udp::Endpoint(localAddress, 0), netif);
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200364
Eric Newberry0c841642018-01-17 15:01:00 -0700365 GenericLinkService::Options options;
366 options.allowCongestionMarking = m_wantCongestionMarking;
367 auto linkService = make_unique<GenericLinkService>(options);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500368 auto transport = make_unique<MulticastUdpTransport>(mcastEp, std::move(rxSock), std::move(txSock),
Teng Liangfe4fce32017-03-29 04:49:38 +0000369 m_mcastConfig.linkType);
Davide Pesavento46afec42017-05-28 14:28:47 -0400370 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700371
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500372 m_mcastFaces[localEp] = face;
373 connectFaceClosedSignal(*face, [this, localEp] { m_mcastFaces.erase(localEp); });
Giulio Grassi624f6c62014-02-18 19:42:14 +0100374
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400375 // Associate with the first available channel of the same protocol family
376 auto channelIt = std::find_if(m_channels.begin(), m_channels.end(),
377 [isV4 = localEp.address().is_v4()] (const auto& it) {
378 return it.first.address().is_v4() == isV4;
379 });
380 face->setChannel(channelIt != m_channels.end() ? channelIt->second : nullptr);
381
Davide Pesavento292e5e12015-03-13 02:08:33 +0100382 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100383}
384
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400385static std::optional<ip::address>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500386pickAddress(const net::NetworkInterface& netif, net::AddressFamily af)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100387{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400388 for (const auto& na : netif.getNetworkAddresses()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500389 if (na.getFamily() == af &&
390 (na.getScope() == net::AddressScope::LINK || na.getScope() == net::AddressScope::GLOBAL)) {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400391 return na.getIp();
Junxiao Shibbace1d2017-08-06 20:03:37 +0000392 }
393 }
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400394 return std::nullopt;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000395}
396
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500397std::vector<shared_ptr<Face>>
Davide Pesaventobb734df2017-10-24 18:05:36 -0400398UdpFactory::applyMcastConfigToNetif(const shared_ptr<const net::NetworkInterface>& netif)
Junxiao Shibbace1d2017-08-06 20:03:37 +0000399{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400400 BOOST_ASSERT(netif != nullptr);
401
Junxiao Shibbace1d2017-08-06 20:03:37 +0000402 if (!m_mcastConfig.isEnabled) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500403 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000404 }
405
406 if (!netif->isUp()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500407 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is down");
408 return {};
409 }
410
411 if (netif->isLoopback()) {
412 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is loopback");
413 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000414 }
415
416 if (!netif->canMulticast()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500417 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif cannot multicast");
418 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000419 }
420
421 if (!m_mcastConfig.netifPredicate(*netif)) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500422 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": rejected by whitelist/blacklist");
423 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000424 }
425
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500426 std::vector<ip::address> addrs;
427 for (auto af : {net::AddressFamily::V4, net::AddressFamily::V6}) {
428 auto addr = pickAddress(*netif, af);
429 if (addr)
430 addrs.push_back(*addr);
Junxiao Shibbace1d2017-08-06 20:03:37 +0000431 }
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500432
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500433 if (addrs.empty()) {
434 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": no viable IP address");
435 // keep an eye on new addresses
436 m_netifConns[netif->getIndex()].addrAddConn =
Davide Pesavento412c9822021-07-02 00:21:05 -0400437 netif->onAddressAdded.connect([=] (auto&&...) { this->applyMcastConfigToNetif(netif); });
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500438 return {};
439 }
440
441 NFD_LOG_DEBUG("Creating multicast faces on " << netif->getName());
442
443 std::vector<shared_ptr<Face>> faces;
444 for (const auto& addr : addrs) {
445 auto face = this->createMulticastFace(netif, addr,
446 addr.is_v4() ? m_mcastConfig.group : m_mcastConfig.groupV6);
447 if (face->getId() == INVALID_FACEID) {
448 // new face: register with forwarding
449 this->addFace(face);
450 }
451 faces.push_back(std::move(face));
452 }
453
454 return faces;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000455}
456
Junxiao Shi64d99f22017-01-21 23:06:36 +0000457void
Davide Pesaventod27841b2018-11-13 00:22:24 -0500458UdpFactory::applyMcastConfig(const FaceSystem::ConfigContext&)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000459{
460 // collect old faces
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500461 std::set<shared_ptr<Face>> facesToClose;
462 boost::copy(m_mcastFaces | boost::adaptors::map_values,
463 std::inserter(facesToClose, facesToClose.end()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000464
Junxiao Shibbace1d2017-08-06 20:03:37 +0000465 // create faces if requested by config
466 for (const auto& netif : netmon->listNetworkInterfaces()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500467 auto facesToKeep = this->applyMcastConfigToNetif(netif);
468 for (const auto& face : facesToKeep) {
Junxiao Shibbace1d2017-08-06 20:03:37 +0000469 // don't destroy face
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500470 facesToClose.erase(face);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000471 }
472 }
473
474 // destroy old faces that are not needed in new configuration
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500475 for (const auto& face : facesToClose) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000476 face->close();
477 }
478}
479
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400480} // namespace nfd::face