blob: f44e4e9585fbe968cf1a63c3fe4ffe1219f9d132 [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/*
Junxiao Shi64d99f22017-01-21 23:06:36 +00003 * Copyright (c) 2014-2017, 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"
Junxiao Shi64d99f22017-01-21 23:06:36 +000030#include <boost/range/adaptors.hpp>
31#include <boost/range/algorithm/copy.hpp>
Giulio Grassi624f6c62014-02-18 19:42:14 +010032
Davide Pesavento292e5e12015-03-13 02:08:33 +010033#ifdef __linux__
34#include <cerrno> // for errno
35#include <cstring> // for std::strerror()
36#include <sys/socket.h> // for setsockopt()
Junxiao Shi64d99f22017-01-21 23:06:36 +000037#endif // __linux__
Giulio Grassi6d7176d2014-04-16 16:08:48 +020038
Giulio Grassi624f6c62014-02-18 19:42:14 +010039namespace nfd {
Junxiao Shi64d99f22017-01-21 23:06:36 +000040namespace face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010041
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020042namespace ip = boost::asio::ip;
Junxiao Shi79494162014-04-02 18:25:11 -070043
Giulio Grassi624f6c62014-02-18 19:42:14 +010044NFD_LOG_INIT("UdpFactory");
Junxiao Shib47247d2017-01-24 15:09:16 +000045NFD_REGISTER_PROTOCOL_FACTORY(UdpFactory);
46
47const std::string&
48UdpFactory::getId()
49{
50 static std::string id("udp");
51 return id;
52}
53
Junxiao Shi0ba6d642017-07-17 00:53:22 +000054UdpFactory::UdpFactory(const CtorParams& params)
55 : ProtocolFactory(params)
56{
Junxiao Shibbace1d2017-08-06 20:03:37 +000057 m_netifAddConn = netmon->onInterfaceAdded.connect(bind(&UdpFactory::applyMcastConfigToNetif, this, _1));
Junxiao Shi0ba6d642017-07-17 00:53:22 +000058}
Giulio Grassi624f6c62014-02-18 19:42:14 +010059
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060060void
Junxiao Shi64d99f22017-01-21 23:06:36 +000061UdpFactory::processConfig(OptionalConfigSection configSection,
62 FaceSystem::ConfigContext& context)
63{
64 // udp
65 // {
66 // port 6363
67 // enable_v4 yes
68 // enable_v6 yes
69 // idle_timeout 600
70 // keep_alive_interval 25 ; acceptable but ignored
71 // mcast yes
72 // mcast_group 224.0.23.170
73 // mcast_port 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
84 uint16_t port = 6363;
85 bool enableV4 = false;
86 bool enableV6 = false;
87 uint32_t idleTimeout = 600;
88 MulticastConfig mcastConfig;
89
90 if (configSection) {
91 // These default to 'yes' but only if face_system.udp section is present
92 enableV4 = enableV6 = mcastConfig.isEnabled = true;
93
94 for (const auto& pair : *configSection) {
95 const std::string& key = pair.first;
96 const ConfigSection& value = pair.second;
97
98 if (key == "port") {
99 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp");
100 }
101 else if (key == "enable_v4") {
102 enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp");
103 }
104 else if (key == "enable_v6") {
105 enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp");
106 }
107 else if (key == "idle_timeout") {
108 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp");
109 }
110 else if (key == "keep_alive_interval") {
111 // ignored
112 }
113 else if (key == "mcast") {
114 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp");
115 }
116 else if (key == "mcast_group") {
117 const std::string& valueStr = value.get_value<std::string>();
118 boost::system::error_code ec;
119 mcastConfig.group.address(boost::asio::ip::address_v4::from_string(valueStr, ec));
120 if (ec) {
121 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
122 valueStr + "' cannot be parsed as an IPv4 address"));
123 }
124 else if (!mcastConfig.group.address().is_multicast()) {
125 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
126 valueStr + "' is not a multicast address"));
127 }
128 }
129 else if (key == "mcast_port") {
130 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
131 }
Teng Liangfe4fce32017-03-29 04:49:38 +0000132 else if (key == "mcast_ad_hoc") {
133 bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.udp");
134 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
135 }
Junxiao Shic31080d2017-01-24 15:10:12 +0000136 else if (key == "whitelist") {
137 mcastConfig.netifPredicate.parseWhitelist(value);
138 }
139 else if (key == "blacklist") {
140 mcastConfig.netifPredicate.parseBlacklist(value);
141 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000142 else {
143 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.udp." + key));
144 }
145 }
146
147 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
148 BOOST_THROW_EXCEPTION(ConfigFile::Error(
149 "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. "
150 "Remove face_system.udp section to disable UDP channels or enable at least one of them."));
151 }
152 }
153
Junxiao Shi79a92082017-08-08 02:40:59 +0000154 if (context.isDryRun) {
155 return;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000156 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000157
158 if (enableV4) {
159 udp::Endpoint endpoint(ip::udp::v4(), port);
160 shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
161 if (!v4Channel->isListening()) {
162 v4Channel->listen(this->addFace, nullptr);
163 }
164 providedSchemes.insert("udp");
165 providedSchemes.insert("udp4");
166 }
167 else if (providedSchemes.count("udp4") > 0) {
168 NFD_LOG_WARN("Cannot close udp4 channel after its creation");
169 }
170
171 if (enableV6) {
172 udp::Endpoint endpoint(ip::udp::v6(), port);
173 shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
174 if (!v6Channel->isListening()) {
175 v6Channel->listen(this->addFace, nullptr);
176 }
177 providedSchemes.insert("udp");
178 providedSchemes.insert("udp6");
179 }
180 else if (providedSchemes.count("udp6") > 0) {
181 NFD_LOG_WARN("Cannot close udp6 channel after its creation");
182 }
183
184 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
185 if (mcastConfig.isEnabled) {
186 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
187 }
188 else {
189 NFD_LOG_INFO("disabling multicast");
190 }
191 }
192 else if (mcastConfig.isEnabled) {
193 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
194 NFD_LOG_WARN("Cannot change ad hoc setting on existing faces");
195 }
196 if (m_mcastConfig.group != mcastConfig.group) {
197 NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group <<
198 " to " << mcastConfig.group);
199 }
200 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
201 NFD_LOG_INFO("changing whitelist/blacklist");
202 }
203 }
204
205 // Even if there's no configuration change, we still need to re-apply configuration because
206 // netifs may have changed.
207 m_mcastConfig = mcastConfig;
208 this->applyMcastConfig(context);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000209}
210
211void
Eric Newberry944f38b2017-07-20 20:54:22 -0400212UdpFactory::createFace(const CreateFaceParams& params,
Junxiao Shi64d99f22017-01-21 23:06:36 +0000213 const FaceCreatedCallback& onCreated,
214 const FaceCreationFailedCallback& onFailure)
215{
Eric Newberry944f38b2017-07-20 20:54:22 -0400216 BOOST_ASSERT(params.remoteUri.isCanonical());
Eric Newberry78e32b02017-04-01 14:34:44 +0000217
Eric Newberry944f38b2017-07-20 20:54:22 -0400218 if (params.localUri) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000219 NFD_LOG_TRACE("Cannot create unicast UDP face with LocalUri");
220 onFailure(406, "Unicast UDP faces cannot be created with a LocalUri");
221 return;
222 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000223
Eric Newberry944f38b2017-07-20 20:54:22 -0400224 if (params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000225 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
Davide Pesavento46afec42017-05-28 14:28:47 -0400226 onFailure(406, "Outgoing UDP faces do not support on-demand persistency");
Junxiao Shi64d99f22017-01-21 23:06:36 +0000227 return;
228 }
229
Eric Newberry944f38b2017-07-20 20:54:22 -0400230 udp::Endpoint endpoint(ip::address::from_string(params.remoteUri.getHost()),
231 boost::lexical_cast<uint16_t>(params.remoteUri.getPort()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000232
233 if (endpoint.address().is_multicast()) {
234 NFD_LOG_TRACE("createFace does not support multicast faces");
235 onFailure(406, "Cannot create multicast UDP faces");
236 return;
237 }
238
Eric Newberry2642cd22017-07-13 21:34:53 -0400239 if (params.wantLocalFields) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000240 // UDP faces are never local
241 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
242 onFailure(406, "Local fields can only be enabled on faces with local scope");
243 return;
244 }
245
246 // very simple logic for now
247 for (const auto& i : m_channels) {
248 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
249 (i.first.address().is_v6() && endpoint.address().is_v6())) {
Eric Newberry2642cd22017-07-13 21:34:53 -0400250 i.second->connect(endpoint, params.persistency, params.wantLpReliability,
251 onCreated, onFailure);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000252 return;
253 }
254 }
255
Davide Pesavento46afec42017-05-28 14:28:47 -0400256 NFD_LOG_TRACE("No channels available to connect to " << endpoint);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000257 onFailure(504, "No channels available to connect");
258}
259
Giulio Grassi624f6c62014-02-18 19:42:14 +0100260shared_ptr<UdpChannel>
Davide Pesavento46afec42017-05-28 14:28:47 -0400261UdpFactory::createChannel(const udp::Endpoint& localEndpoint,
262 time::nanoseconds idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100263{
Davide Pesavento46afec42017-05-28 14:28:47 -0400264 auto it = m_channels.find(localEndpoint);
265 if (it != m_channels.end())
266 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100267
Davide Pesavento46afec42017-05-28 14:28:47 -0400268 if (localEndpoint.address().is_multicast()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200269 BOOST_THROW_EXCEPTION(Error("createChannel is only for unicast channels. The provided endpoint "
270 "is multicast. Use createMulticastFace to create a multicast face"));
271 }
272
Yukai Tu0a49d342015-09-13 12:54:22 +0800273 // check if the endpoint is already used by a multicast face
Davide Pesavento46afec42017-05-28 14:28:47 -0400274 if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700275 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP unicast channel, local "
276 "endpoint is already allocated for a UDP multicast face"));
Yukai Tu0a49d342015-09-13 12:54:22 +0800277 }
Junxiao Shi79494162014-04-02 18:25:11 -0700278
Davide Pesavento46afec42017-05-28 14:28:47 -0400279 auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout);
280 m_channels[localEndpoint] = channel;
Davide Pesavento46afec42017-05-28 14:28:47 -0400281
Giulio Grassi624f6c62014-02-18 19:42:14 +0100282 return channel;
283}
284
285shared_ptr<UdpChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200286UdpFactory::createChannel(const std::string& localIp, const std::string& localPort,
Davide Pesavento46afec42017-05-28 14:28:47 -0400287 time::nanoseconds idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100288{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200289 udp::Endpoint endpoint(ip::address::from_string(localIp),
290 boost::lexical_cast<uint16_t>(localPort));
Davide Pesavento46afec42017-05-28 14:28:47 -0400291 return createChannel(endpoint, idleTimeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100292}
293
Junxiao Shi64d99f22017-01-21 23:06:36 +0000294std::vector<shared_ptr<const Channel>>
295UdpFactory::getChannels() const
296{
297 return getChannelsFromMap(m_channels);
298}
299
Junxiao Shicde37ad2015-12-24 01:02:05 -0700300shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100301UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200302 const udp::Endpoint& multicastEndpoint,
Davide Pesavento46afec42017-05-28 14:28:47 -0400303 const std::string& networkInterfaceName)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100304{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100305 // checking if the local and multicast endpoints are already in use for a multicast face
Davide Pesavento46afec42017-05-28 14:28:47 -0400306 auto it = m_mcastFaces.find(localEndpoint);
307 if (it != m_mcastFaces.end()) {
308 if (it->second->getRemoteUri() == FaceUri(multicastEndpoint))
309 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100310 else
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700311 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local "
312 "endpoint is already allocated for a UDP multicast face "
313 "on a different multicast group"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100314 }
Junxiao Shi79494162014-04-02 18:25:11 -0700315
Davide Pesavento292e5e12015-03-13 02:08:33 +0100316 // checking if the local endpoint is already in use for a unicast channel
Davide Pesavento46afec42017-05-28 14:28:47 -0400317 if (m_channels.find(localEndpoint) != m_channels.end()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700318 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local "
319 "endpoint is already allocated for a UDP unicast channel"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100320 }
321
322 if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700323 BOOST_THROW_EXCEPTION(Error("IPv6 multicast is not supported yet. Please provide an IPv4 "
324 "address"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100325 }
Junxiao Shi79494162014-04-02 18:25:11 -0700326
Giulio Grassi624f6c62014-02-18 19:42:14 +0100327 if (localEndpoint.port() != multicastEndpoint.port()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700328 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
329 "both endpoints should have the same port number. "));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100330 }
331
332 if (!multicastEndpoint.address().is_multicast()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700333 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
334 "the multicast group given as input is not a multicast address"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100335 }
336
Davide Pesavento292e5e12015-03-13 02:08:33 +0100337 ip::udp::socket receiveSocket(getGlobalIoService());
338 receiveSocket.open(multicastEndpoint.protocol());
339 receiveSocket.set_option(ip::udp::socket::reuse_address(true));
340 receiveSocket.bind(multicastEndpoint);
Junxiao Shi79494162014-04-02 18:25:11 -0700341
Davide Pesavento292e5e12015-03-13 02:08:33 +0100342 ip::udp::socket sendSocket(getGlobalIoService());
343 sendSocket.open(multicastEndpoint.protocol());
344 sendSocket.set_option(ip::udp::socket::reuse_address(true));
345 sendSocket.set_option(ip::multicast::enable_loopback(false));
346 sendSocket.bind(udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port()));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200347 if (localEndpoint.address() != ip::address_v4::any())
Davide Pesavento292e5e12015-03-13 02:08:33 +0100348 sendSocket.set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100349
Davide Pesavento292e5e12015-03-13 02:08:33 +0100350 sendSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
351 localEndpoint.address().to_v4()));
352 receiveSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700353 localEndpoint.address().to_v4()));
354
Davide Pesavento292e5e12015-03-13 02:08:33 +0100355#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000356 // On Linux, if there is more than one multicast UDP face for the same multicast
357 // group but they are bound to different network interfaces, the socket needs
358 // to be bound to the specific interface using SO_BINDTODEVICE, otherwise the
359 // face will receive all packets sent to the other interfaces as well.
360 // This happens only on Linux. On macOS, the ip::multicast::join_group option
361 // is enough to get the desired behaviour.
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200362 if (!networkInterfaceName.empty()) {
Davide Pesavento292e5e12015-03-13 02:08:33 +0100363 if (::setsockopt(receiveSocket.native_handle(), SOL_SOCKET, SO_BINDTODEVICE,
364 networkInterfaceName.c_str(), networkInterfaceName.size() + 1) < 0) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700365 BOOST_THROW_EXCEPTION(Error("Cannot bind multicast face to " + networkInterfaceName +
366 ": " + std::strerror(errno)));
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200367 }
368 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000369#endif // __linux__
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200370
Junxiao Shi64d99f22017-01-21 23:06:36 +0000371 auto linkService = make_unique<GenericLinkService>();
372 auto transport = make_unique<MulticastUdpTransport>(localEndpoint, multicastEndpoint,
373 std::move(receiveSocket),
Teng Liangfe4fce32017-03-29 04:49:38 +0000374 std::move(sendSocket),
375 m_mcastConfig.linkType);
Davide Pesavento46afec42017-05-28 14:28:47 -0400376 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700377
Junxiao Shi64d99f22017-01-21 23:06:36 +0000378 m_mcastFaces[localEndpoint] = face;
379 connectFaceClosedSignal(*face, [this, localEndpoint] { m_mcastFaces.erase(localEndpoint); });
Giulio Grassi624f6c62014-02-18 19:42:14 +0100380
Davide Pesavento292e5e12015-03-13 02:08:33 +0100381 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100382}
383
Junxiao Shicde37ad2015-12-24 01:02:05 -0700384shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100385UdpFactory::createMulticastFace(const std::string& localIp,
386 const std::string& multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200387 const std::string& multicastPort,
Davide Pesavento46afec42017-05-28 14:28:47 -0400388 const std::string& networkInterfaceName)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100389{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100390 udp::Endpoint localEndpoint(ip::address::from_string(localIp),
Chengyu Fan4381fb62015-01-14 11:37:04 -0700391 boost::lexical_cast<uint16_t>(multicastPort));
Davide Pesavento292e5e12015-03-13 02:08:33 +0100392 udp::Endpoint multicastEndpoint(ip::address::from_string(multicastIp),
Chengyu Fan4381fb62015-01-14 11:37:04 -0700393 boost::lexical_cast<uint16_t>(multicastPort));
Chengyu Fan4381fb62015-01-14 11:37:04 -0700394 return createMulticastFace(localEndpoint, multicastEndpoint, networkInterfaceName);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100395}
396
Junxiao Shibbace1d2017-08-06 20:03:37 +0000397static ndn::optional<ndn::net::NetworkAddress>
398getV4Address(const ndn::net::NetworkInterface& netif)
399{
400 using namespace ndn::net;
401 for (const NetworkAddress& na : netif.getNetworkAddresses()) {
402 if (na.getFamily() == AddressFamily::V4 && na.getScope() != AddressScope::NOWHERE) {
403 return na;
404 }
405 }
406 return ndn::nullopt;
407}
408
409shared_ptr<Face>
410UdpFactory::applyMcastConfigToNetif(const shared_ptr<const ndn::net::NetworkInterface>& netif)
411{
412 if (!m_mcastConfig.isEnabled) {
413 return nullptr;
414 }
415
416 if (!netif->isUp()) {
417 NFD_LOG_DEBUG("Not creating multicast face on " << netif->getName() << ": netif is down");
418 return nullptr;
419 }
420
421 if (!netif->canMulticast()) {
422 NFD_LOG_DEBUG("Not creating multicast face on " << netif->getName() << ": netif cannot multicast");
423 return nullptr;
424 }
425
426 auto address = getV4Address(*netif);
427 if (!address) {
428 NFD_LOG_DEBUG("Not creating multicast face on " << netif->getName() << ": no IPv4 address");
429 // keep an eye on new addresses
430 m_netifConns[netif->getIndex()].addrAddConn =
431 netif->onAddressAdded.connectSingleShot(bind(&UdpFactory::applyMcastConfigToNetif, this, netif));
432 return nullptr;
433 }
434
435 if (!m_mcastConfig.netifPredicate(*netif)) {
Junxiao Shi79a92082017-08-08 02:40:59 +0000436 NFD_LOG_DEBUG("Not creating multicast face on " << netif->getName() << ": rejected by whitelist/blacklist");
Junxiao Shibbace1d2017-08-06 20:03:37 +0000437 return nullptr;
438 }
439
440 NFD_LOG_DEBUG("Creating multicast face on " << netif->getName());
441 udp::Endpoint localEndpoint(address->getIp(), m_mcastConfig.group.port());
442 auto face = this->createMulticastFace(localEndpoint, m_mcastConfig.group, netif->getName());
Junxiao Shi79a92082017-08-08 02:40:59 +0000443 // ifname is only used on Linux. It is not required if there is only one multicast-capable netif,
444 // but it is always supplied because a new netif can be added at anytime.
Junxiao Shibbace1d2017-08-06 20:03:37 +0000445
446 if (face->getId() == INVALID_FACEID) {
447 // new face: register with forwarding
448 this->addFace(face);
449 }
450 return face;
451}
452
Junxiao Shi64d99f22017-01-21 23:06:36 +0000453void
Junxiao Shibbace1d2017-08-06 20:03:37 +0000454UdpFactory::applyMcastConfig(const FaceSystem::ConfigContext& context)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000455{
456 // collect old faces
457 std::set<shared_ptr<Face>> oldFaces;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000458 boost::copy(m_mcastFaces | boost::adaptors::map_values, std::inserter(oldFaces, oldFaces.end()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000459
Junxiao Shibbace1d2017-08-06 20:03:37 +0000460 // create faces if requested by config
461 for (const auto& netif : netmon->listNetworkInterfaces()) {
462 auto face = this->applyMcastConfigToNetif(netif);
463 if (face != nullptr) {
464 // don't destroy face
465 oldFaces.erase(face);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000466 }
467 }
468
469 // destroy old faces that are not needed in new configuration
470 for (const auto& face : oldFaces) {
471 face->close();
472 }
473}
474
475} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100476} // namespace nfd