blob: c9b4ac4d584572a9a72a82861db4f931ec714fdf [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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");
45
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060046void
Junxiao Shi64d99f22017-01-21 23:06:36 +000047UdpFactory::processConfig(OptionalConfigSection configSection,
48 FaceSystem::ConfigContext& context)
49{
50 // udp
51 // {
52 // port 6363
53 // enable_v4 yes
54 // enable_v6 yes
55 // idle_timeout 600
56 // keep_alive_interval 25 ; acceptable but ignored
57 // mcast yes
58 // mcast_group 224.0.23.170
59 // mcast_port 56363
60 // }
61
62 uint16_t port = 6363;
63 bool enableV4 = false;
64 bool enableV6 = false;
65 uint32_t idleTimeout = 600;
66 MulticastConfig mcastConfig;
67
68 if (configSection) {
69 // These default to 'yes' but only if face_system.udp section is present
70 enableV4 = enableV6 = mcastConfig.isEnabled = true;
71
72 for (const auto& pair : *configSection) {
73 const std::string& key = pair.first;
74 const ConfigSection& value = pair.second;
75
76 if (key == "port") {
77 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp");
78 }
79 else if (key == "enable_v4") {
80 enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp");
81 }
82 else if (key == "enable_v6") {
83 enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp");
84 }
85 else if (key == "idle_timeout") {
86 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp");
87 }
88 else if (key == "keep_alive_interval") {
89 // ignored
90 }
91 else if (key == "mcast") {
92 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp");
93 }
94 else if (key == "mcast_group") {
95 const std::string& valueStr = value.get_value<std::string>();
96 boost::system::error_code ec;
97 mcastConfig.group.address(boost::asio::ip::address_v4::from_string(valueStr, ec));
98 if (ec) {
99 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
100 valueStr + "' cannot be parsed as an IPv4 address"));
101 }
102 else if (!mcastConfig.group.address().is_multicast()) {
103 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
104 valueStr + "' is not a multicast address"));
105 }
106 }
107 else if (key == "mcast_port") {
108 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
109 }
110 else {
111 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.udp." + key));
112 }
113 }
114
115 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
116 BOOST_THROW_EXCEPTION(ConfigFile::Error(
117 "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. "
118 "Remove face_system.udp section to disable UDP channels or enable at least one of them."));
119 }
120 }
121
122 if (!context.isDryRun) {
123 if (enableV4) {
124 udp::Endpoint endpoint(ip::udp::v4(), port);
125 shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
126 if (!v4Channel->isListening()) {
127 v4Channel->listen(context.addFace, nullptr);
128 }
129 providedSchemes.insert("udp");
130 providedSchemes.insert("udp4");
131 }
132 else if (providedSchemes.count("udp4") > 0) {
133 NFD_LOG_WARN("Cannot close udp4 channel after its creation");
134 }
135
136 if (enableV6) {
137 udp::Endpoint endpoint(ip::udp::v6(), port);
138 shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
139 if (!v6Channel->isListening()) {
140 v6Channel->listen(context.addFace, nullptr);
141 }
142 providedSchemes.insert("udp");
143 providedSchemes.insert("udp6");
144 }
145 else if (providedSchemes.count("udp6") > 0) {
146 NFD_LOG_WARN("Cannot close udp6 channel after its creation");
147 }
148
149 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
150 if (mcastConfig.isEnabled) {
151 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
152 }
153 else {
154 NFD_LOG_INFO("disabling multicast");
155 }
156 }
157 else if (m_mcastConfig.group != mcastConfig.group) {
158 NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group <<
159 " to " << mcastConfig.group);
160 }
161 else {
162 // There's no configuration change, but we still need to re-apply configuration because
163 // netifs may have changed.
164 }
165
166 m_mcastConfig = mcastConfig;
167 this->applyMulticastConfig(context);
168 }
169}
170
171void
172UdpFactory::createFace(const FaceUri& uri,
173 ndn::nfd::FacePersistency persistency,
174 bool wantLocalFieldsEnabled,
175 const FaceCreatedCallback& onCreated,
176 const FaceCreationFailedCallback& onFailure)
177{
178 BOOST_ASSERT(uri.isCanonical());
179
180 if (persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
181 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
182 onFailure(406, "Outgoing unicast UDP faces do not support on-demand persistency");
183 return;
184 }
185
186 udp::Endpoint endpoint(ip::address::from_string(uri.getHost()),
187 boost::lexical_cast<uint16_t>(uri.getPort()));
188
189 if (endpoint.address().is_multicast()) {
190 NFD_LOG_TRACE("createFace does not support multicast faces");
191 onFailure(406, "Cannot create multicast UDP faces");
192 return;
193 }
194
195 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
196 NFD_LOG_TRACE("Requested endpoint is prohibited "
197 "(reserved by this NFD or disallowed by face management protocol)");
198 onFailure(406, "Requested endpoint is prohibited");
199 return;
200 }
201
202 if (wantLocalFieldsEnabled) {
203 // UDP faces are never local
204 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
205 onFailure(406, "Local fields can only be enabled on faces with local scope");
206 return;
207 }
208
209 // very simple logic for now
210 for (const auto& i : m_channels) {
211 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
212 (i.first.address().is_v6() && endpoint.address().is_v6())) {
213 i.second->connect(endpoint, persistency, onCreated, onFailure);
214 return;
215 }
216 }
217
218 NFD_LOG_TRACE("No channels available to connect to " + boost::lexical_cast<std::string>(endpoint));
219 onFailure(504, "No channels available to connect");
220}
221
222void
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600223UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint)
224{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200225 if (endpoint.address().is_v4() &&
226 endpoint.address() == ip::address_v4::any()) {
227 prohibitAllIpv4Endpoints(endpoint.port());
228 }
229 else if (endpoint.address().is_v6() &&
230 endpoint.address() == ip::address_v6::any()) {
231 prohibitAllIpv6Endpoints(endpoint.port());
232 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600233
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800234 NFD_LOG_TRACE("prohibiting UDP " << endpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600235 m_prohibitedEndpoints.insert(endpoint);
236}
237
238void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200239UdpFactory::prohibitAllIpv4Endpoints(uint16_t port)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600240{
Davide Pesaventob499a602014-11-18 22:36:56 +0100241 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200242 for (const auto& addr : nic.ipv4Addresses) {
243 if (addr != ip::address_v4::any()) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800244 prohibitEndpoint(udp::Endpoint(addr, port));
245 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600246 }
247
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200248 if (nic.isBroadcastCapable() &&
249 nic.broadcastAddress != ip::address_v4::any()) {
Davide Pesaventob499a602014-11-18 22:36:56 +0100250 prohibitEndpoint(udp::Endpoint(nic.broadcastAddress, port));
251 }
252 }
253
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200254 prohibitEndpoint(udp::Endpoint(ip::address_v4::broadcast(), port));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600255}
256
257void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200258UdpFactory::prohibitAllIpv6Endpoints(uint16_t port)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600259{
Davide Pesaventob499a602014-11-18 22:36:56 +0100260 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200261 for (const auto& addr : nic.ipv6Addresses) {
262 if (addr != ip::address_v6::any()) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800263 prohibitEndpoint(udp::Endpoint(addr, port));
264 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600265 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100266 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600267}
268
Giulio Grassi624f6c62014-02-18 19:42:14 +0100269shared_ptr<UdpChannel>
270UdpFactory::createChannel(const udp::Endpoint& endpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700271 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100272{
Davide Pesaventob499a602014-11-18 22:36:56 +0100273 NFD_LOG_DEBUG("Creating unicast channel " << endpoint);
Junxiao Shi79494162014-04-02 18:25:11 -0700274
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200275 auto channel = findChannel(endpoint);
276 if (channel)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100277 return channel;
278
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200279 if (endpoint.address().is_multicast()) {
280 BOOST_THROW_EXCEPTION(Error("createChannel is only for unicast channels. The provided endpoint "
281 "is multicast. Use createMulticastFace to create a multicast face"));
282 }
283
Yukai Tu0a49d342015-09-13 12:54:22 +0800284 // check if the endpoint is already used by a multicast face
285 auto face = findMulticastFace(endpoint);
286 if (face) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700287 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP unicast channel, local "
288 "endpoint is already allocated for a UDP multicast face"));
Yukai Tu0a49d342015-09-13 12:54:22 +0800289 }
Junxiao Shi79494162014-04-02 18:25:11 -0700290
Yumin Xiaab497452016-05-10 20:23:24 +0800291 channel = std::make_shared<UdpChannel>(endpoint, timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100292 m_channels[endpoint] = channel;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600293 prohibitEndpoint(endpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100294
295 return channel;
296}
297
298shared_ptr<UdpChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200299UdpFactory::createChannel(const std::string& localIp, const std::string& localPort,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700300 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100301{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200302 udp::Endpoint endpoint(ip::address::from_string(localIp),
303 boost::lexical_cast<uint16_t>(localPort));
Alexander Afanasyev0e156df2015-01-26 22:33:43 -0800304 return createChannel(endpoint, timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100305}
306
Junxiao Shi64d99f22017-01-21 23:06:36 +0000307std::vector<shared_ptr<const Channel>>
308UdpFactory::getChannels() const
309{
310 return getChannelsFromMap(m_channels);
311}
312
313shared_ptr<UdpChannel>
314UdpFactory::findChannel(const udp::Endpoint& localEndpoint) const
315{
316 auto i = m_channels.find(localEndpoint);
317 if (i != m_channels.end())
318 return i->second;
319 else
320 return nullptr;
321}
322
Junxiao Shicde37ad2015-12-24 01:02:05 -0700323shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100324UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200325 const udp::Endpoint& multicastEndpoint,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100326 const std::string& networkInterfaceName/* = ""*/)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100327{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100328 // checking if the local and multicast endpoints are already in use for a multicast face
Yukai Tu0a49d342015-09-13 12:54:22 +0800329 auto face = findMulticastFace(localEndpoint);
330 if (face) {
331 if (face->getRemoteUri() == FaceUri(multicastEndpoint))
Davide Pesavento292e5e12015-03-13 02:08:33 +0100332 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100333 else
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700334 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local "
335 "endpoint is already allocated for a UDP multicast face "
336 "on a different multicast group"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100337 }
Junxiao Shi79494162014-04-02 18:25:11 -0700338
Davide Pesavento292e5e12015-03-13 02:08:33 +0100339 // checking if the local endpoint is already in use for a unicast channel
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200340 auto unicastCh = findChannel(localEndpoint);
341 if (unicastCh) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700342 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local "
343 "endpoint is already allocated for a UDP unicast channel"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100344 }
345
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600346 if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700347 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
348 "remote endpoint is owned by this NFD instance"));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600349 }
350
Giulio Grassi624f6c62014-02-18 19:42:14 +0100351 if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700352 BOOST_THROW_EXCEPTION(Error("IPv6 multicast is not supported yet. Please provide an IPv4 "
353 "address"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100354 }
Junxiao Shi79494162014-04-02 18:25:11 -0700355
Giulio Grassi624f6c62014-02-18 19:42:14 +0100356 if (localEndpoint.port() != multicastEndpoint.port()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700357 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
358 "both endpoints should have the same port number. "));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100359 }
360
361 if (!multicastEndpoint.address().is_multicast()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700362 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
363 "the multicast group given as input is not a multicast address"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100364 }
365
Davide Pesavento292e5e12015-03-13 02:08:33 +0100366 ip::udp::socket receiveSocket(getGlobalIoService());
367 receiveSocket.open(multicastEndpoint.protocol());
368 receiveSocket.set_option(ip::udp::socket::reuse_address(true));
369 receiveSocket.bind(multicastEndpoint);
Junxiao Shi79494162014-04-02 18:25:11 -0700370
Davide Pesavento292e5e12015-03-13 02:08:33 +0100371 ip::udp::socket sendSocket(getGlobalIoService());
372 sendSocket.open(multicastEndpoint.protocol());
373 sendSocket.set_option(ip::udp::socket::reuse_address(true));
374 sendSocket.set_option(ip::multicast::enable_loopback(false));
375 sendSocket.bind(udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port()));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200376 if (localEndpoint.address() != ip::address_v4::any())
Davide Pesavento292e5e12015-03-13 02:08:33 +0100377 sendSocket.set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100378
Davide Pesavento292e5e12015-03-13 02:08:33 +0100379 sendSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
380 localEndpoint.address().to_v4()));
381 receiveSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700382 localEndpoint.address().to_v4()));
383
Davide Pesavento292e5e12015-03-13 02:08:33 +0100384#ifdef __linux__
385 /*
386 * On Linux, if there is more than one MulticastUdpFace for the same multicast
387 * group but they are bound to different network interfaces, the socket needs
388 * to be bound to the specific interface using SO_BINDTODEVICE, otherwise the
389 * face will receive all packets sent to the other interfaces as well.
390 * This happens only on Linux. On OS X, the ip::multicast::join_group option
391 * is enough to get the desired behaviour.
392 */
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200393 if (!networkInterfaceName.empty()) {
Davide Pesavento292e5e12015-03-13 02:08:33 +0100394 if (::setsockopt(receiveSocket.native_handle(), SOL_SOCKET, SO_BINDTODEVICE,
395 networkInterfaceName.c_str(), networkInterfaceName.size() + 1) < 0) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700396 BOOST_THROW_EXCEPTION(Error("Cannot bind multicast face to " + networkInterfaceName +
397 ": " + std::strerror(errno)));
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200398 }
399 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000400#endif // __linux__
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200401
Junxiao Shi64d99f22017-01-21 23:06:36 +0000402 auto linkService = make_unique<GenericLinkService>();
403 auto transport = make_unique<MulticastUdpTransport>(localEndpoint, multicastEndpoint,
404 std::move(receiveSocket),
405 std::move(sendSocket));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700406 face = make_shared<Face>(std::move(linkService), std::move(transport));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700407
Junxiao Shi64d99f22017-01-21 23:06:36 +0000408 m_mcastFaces[localEndpoint] = face;
409 connectFaceClosedSignal(*face, [this, localEndpoint] { m_mcastFaces.erase(localEndpoint); });
Giulio Grassi624f6c62014-02-18 19:42:14 +0100410
Davide Pesavento292e5e12015-03-13 02:08:33 +0100411 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100412}
413
Junxiao Shicde37ad2015-12-24 01:02:05 -0700414shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100415UdpFactory::createMulticastFace(const std::string& localIp,
416 const std::string& multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200417 const std::string& multicastPort,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100418 const std::string& networkInterfaceName/* = ""*/)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100419{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100420 udp::Endpoint localEndpoint(ip::address::from_string(localIp),
Chengyu Fan4381fb62015-01-14 11:37:04 -0700421 boost::lexical_cast<uint16_t>(multicastPort));
Davide Pesavento292e5e12015-03-13 02:08:33 +0100422 udp::Endpoint multicastEndpoint(ip::address::from_string(multicastIp),
Chengyu Fan4381fb62015-01-14 11:37:04 -0700423 boost::lexical_cast<uint16_t>(multicastPort));
Chengyu Fan4381fb62015-01-14 11:37:04 -0700424 return createMulticastFace(localEndpoint, multicastEndpoint, networkInterfaceName);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100425}
426
Junxiao Shicde37ad2015-12-24 01:02:05 -0700427shared_ptr<Face>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200428UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint) const
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600429{
Junxiao Shi64d99f22017-01-21 23:06:36 +0000430 auto i = m_mcastFaces.find(localEndpoint);
431 if (i != m_mcastFaces.end())
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200432 return i->second;
433 else
434 return nullptr;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600435}
436
Junxiao Shi64d99f22017-01-21 23:06:36 +0000437void
438UdpFactory::applyMulticastConfig(const FaceSystem::ConfigContext& context)
439{
440 // collect old faces
441 std::set<shared_ptr<Face>> oldFaces;
442 boost::copy(m_mcastFaces | boost::adaptors::map_values,
443 std::inserter(oldFaces, oldFaces.end()));
444
445 if (m_mcastConfig.isEnabled) {
446 // determine interfaces on which faces should be created or retained
447 auto capableNetifRange = context.listNetifs() |
448 boost::adaptors::filtered([this] (const NetworkInterfaceInfo& netif) {
449 return netif.isUp() && netif.isMulticastCapable() &&
450 !netif.ipv4Addresses.empty();
451 });
452
453 bool needIfname = false;
454#ifdef __linux__
455 std::vector<NetworkInterfaceInfo> capableNetifs;
456 boost::copy(capableNetifRange, std::back_inserter(capableNetifs));
457 // on Linux, ifname is needed to create more than one UDP multicast face on the same group
458 needIfname = capableNetifs.size() > 1;
459#else
460 auto& capableNetifs = capableNetifRange;
461#endif // __linux__
462
463 // create faces
464 for (const auto& netif : capableNetifs) {
465 udp::Endpoint localEndpoint(netif.ipv4Addresses.front(), m_mcastConfig.group.port());
466 shared_ptr<Face> face = this->createMulticastFace(localEndpoint, m_mcastConfig.group,
467 needIfname ? netif.name : "");
468 if (face->getId() == INVALID_FACEID) {
469 // new face: register with forwarding
470 context.addFace(face);
471 }
472 else {
473 // existing face: don't destroy
474 oldFaces.erase(face);
475 }
476 }
477 }
478
479 // destroy old faces that are not needed in new configuration
480 for (const auto& face : oldFaces) {
481 face->close();
482 }
483}
484
485} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100486} // namespace nfd