blob: 8303aa81897c633a1cd6c4f503d08886e96e8fa2 [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{
57}
Giulio Grassi624f6c62014-02-18 19:42:14 +010058
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060059void
Junxiao Shi64d99f22017-01-21 23:06:36 +000060UdpFactory::processConfig(OptionalConfigSection configSection,
61 FaceSystem::ConfigContext& context)
62{
63 // udp
64 // {
65 // port 6363
66 // enable_v4 yes
67 // enable_v6 yes
68 // idle_timeout 600
69 // keep_alive_interval 25 ; acceptable but ignored
70 // mcast yes
71 // mcast_group 224.0.23.170
72 // mcast_port 56363
Teng Liangfe4fce32017-03-29 04:49:38 +000073 // mcast_ad_hoc no
Junxiao Shic31080d2017-01-24 15:10:12 +000074 // whitelist
75 // {
76 // *
77 // }
78 // blacklist
79 // {
80 // }
Junxiao Shi64d99f22017-01-21 23:06:36 +000081 // }
82
83 uint16_t port = 6363;
84 bool enableV4 = false;
85 bool enableV6 = false;
86 uint32_t idleTimeout = 600;
87 MulticastConfig mcastConfig;
88
89 if (configSection) {
90 // These default to 'yes' but only if face_system.udp section is present
91 enableV4 = enableV6 = mcastConfig.isEnabled = true;
92
93 for (const auto& pair : *configSection) {
94 const std::string& key = pair.first;
95 const ConfigSection& value = pair.second;
96
97 if (key == "port") {
98 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp");
99 }
100 else if (key == "enable_v4") {
101 enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp");
102 }
103 else if (key == "enable_v6") {
104 enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp");
105 }
106 else if (key == "idle_timeout") {
107 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp");
108 }
109 else if (key == "keep_alive_interval") {
110 // ignored
111 }
112 else if (key == "mcast") {
113 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp");
114 }
115 else if (key == "mcast_group") {
116 const std::string& valueStr = value.get_value<std::string>();
117 boost::system::error_code ec;
118 mcastConfig.group.address(boost::asio::ip::address_v4::from_string(valueStr, ec));
119 if (ec) {
120 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
121 valueStr + "' cannot be parsed as an IPv4 address"));
122 }
123 else if (!mcastConfig.group.address().is_multicast()) {
124 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
125 valueStr + "' is not a multicast address"));
126 }
127 }
128 else if (key == "mcast_port") {
129 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
130 }
Teng Liangfe4fce32017-03-29 04:49:38 +0000131 else if (key == "mcast_ad_hoc") {
132 bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.udp");
133 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
134 }
Junxiao Shic31080d2017-01-24 15:10:12 +0000135 else if (key == "whitelist") {
136 mcastConfig.netifPredicate.parseWhitelist(value);
137 }
138 else if (key == "blacklist") {
139 mcastConfig.netifPredicate.parseBlacklist(value);
140 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000141 else {
142 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.udp." + key));
143 }
144 }
145
146 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
147 BOOST_THROW_EXCEPTION(ConfigFile::Error(
148 "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. "
149 "Remove face_system.udp section to disable UDP channels or enable at least one of them."));
150 }
151 }
152
153 if (!context.isDryRun) {
154 if (enableV4) {
155 udp::Endpoint endpoint(ip::udp::v4(), port);
156 shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
157 if (!v4Channel->isListening()) {
Junxiao Shi2d491752017-07-14 21:32:05 +0000158 v4Channel->listen(this->addFace, nullptr);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000159 }
160 providedSchemes.insert("udp");
161 providedSchemes.insert("udp4");
162 }
163 else if (providedSchemes.count("udp4") > 0) {
164 NFD_LOG_WARN("Cannot close udp4 channel after its creation");
165 }
166
167 if (enableV6) {
168 udp::Endpoint endpoint(ip::udp::v6(), port);
169 shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
170 if (!v6Channel->isListening()) {
Junxiao Shi2d491752017-07-14 21:32:05 +0000171 v6Channel->listen(this->addFace, nullptr);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000172 }
173 providedSchemes.insert("udp");
174 providedSchemes.insert("udp6");
175 }
176 else if (providedSchemes.count("udp6") > 0) {
177 NFD_LOG_WARN("Cannot close udp6 channel after its creation");
178 }
179
180 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
181 if (mcastConfig.isEnabled) {
182 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
183 }
184 else {
185 NFD_LOG_INFO("disabling multicast");
186 }
187 }
Teng Liangfe4fce32017-03-29 04:49:38 +0000188 else if (mcastConfig.isEnabled) {
189 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
190 NFD_LOG_WARN("Cannot change ad hoc setting on existing faces");
191 }
192 if (m_mcastConfig.group != mcastConfig.group) {
193 NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group <<
194 " to " << mcastConfig.group);
195 }
196 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
197 NFD_LOG_INFO("changing whitelist/blacklist");
198 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000199 }
200
Teng Liangfe4fce32017-03-29 04:49:38 +0000201 // Even if there's no configuration change, we still need to re-apply configuration because
202 // netifs may have changed.
Junxiao Shi64d99f22017-01-21 23:06:36 +0000203 m_mcastConfig = mcastConfig;
204 this->applyMulticastConfig(context);
205 }
206}
207
208void
Eric Newberry78e32b02017-04-01 14:34:44 +0000209UdpFactory::createFace(const FaceUri& remoteUri,
210 const ndn::optional<FaceUri>& localUri,
Junxiao Shi64d99f22017-01-21 23:06:36 +0000211 ndn::nfd::FacePersistency persistency,
212 bool wantLocalFieldsEnabled,
213 const FaceCreatedCallback& onCreated,
214 const FaceCreationFailedCallback& onFailure)
215{
Eric Newberry78e32b02017-04-01 14:34:44 +0000216 BOOST_ASSERT(remoteUri.isCanonical());
217
218 if (localUri) {
219 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
224 if (persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
225 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 Newberry78e32b02017-04-01 14:34:44 +0000230 udp::Endpoint endpoint(ip::address::from_string(remoteUri.getHost()),
231 boost::lexical_cast<uint16_t>(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
239 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
240 NFD_LOG_TRACE("Requested endpoint is prohibited "
241 "(reserved by this NFD or disallowed by face management protocol)");
242 onFailure(406, "Requested endpoint is prohibited");
243 return;
244 }
245
246 if (wantLocalFieldsEnabled) {
247 // UDP faces are never local
248 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
249 onFailure(406, "Local fields can only be enabled on faces with local scope");
250 return;
251 }
252
253 // very simple logic for now
254 for (const auto& i : m_channels) {
255 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
256 (i.first.address().is_v6() && endpoint.address().is_v6())) {
257 i.second->connect(endpoint, persistency, onCreated, onFailure);
258 return;
259 }
260 }
261
Davide Pesavento46afec42017-05-28 14:28:47 -0400262 NFD_LOG_TRACE("No channels available to connect to " << endpoint);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000263 onFailure(504, "No channels available to connect");
264}
265
266void
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600267UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint)
268{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200269 if (endpoint.address().is_v4() &&
270 endpoint.address() == ip::address_v4::any()) {
271 prohibitAllIpv4Endpoints(endpoint.port());
272 }
273 else if (endpoint.address().is_v6() &&
274 endpoint.address() == ip::address_v6::any()) {
275 prohibitAllIpv6Endpoints(endpoint.port());
276 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600277
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800278 NFD_LOG_TRACE("prohibiting UDP " << endpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600279 m_prohibitedEndpoints.insert(endpoint);
280}
281
282void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200283UdpFactory::prohibitAllIpv4Endpoints(uint16_t port)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600284{
Davide Pesaventob499a602014-11-18 22:36:56 +0100285 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200286 for (const auto& addr : nic.ipv4Addresses) {
287 if (addr != ip::address_v4::any()) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800288 prohibitEndpoint(udp::Endpoint(addr, port));
289 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600290 }
291
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200292 if (nic.isBroadcastCapable() &&
293 nic.broadcastAddress != ip::address_v4::any()) {
Davide Pesaventob499a602014-11-18 22:36:56 +0100294 prohibitEndpoint(udp::Endpoint(nic.broadcastAddress, port));
295 }
296 }
297
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200298 prohibitEndpoint(udp::Endpoint(ip::address_v4::broadcast(), port));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600299}
300
301void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200302UdpFactory::prohibitAllIpv6Endpoints(uint16_t port)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600303{
Davide Pesaventob499a602014-11-18 22:36:56 +0100304 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200305 for (const auto& addr : nic.ipv6Addresses) {
306 if (addr != ip::address_v6::any()) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800307 prohibitEndpoint(udp::Endpoint(addr, port));
308 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600309 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100310 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600311}
312
Giulio Grassi624f6c62014-02-18 19:42:14 +0100313shared_ptr<UdpChannel>
Davide Pesavento46afec42017-05-28 14:28:47 -0400314UdpFactory::createChannel(const udp::Endpoint& localEndpoint,
315 time::nanoseconds idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100316{
Davide Pesavento46afec42017-05-28 14:28:47 -0400317 auto it = m_channels.find(localEndpoint);
318 if (it != m_channels.end())
319 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100320
Davide Pesavento46afec42017-05-28 14:28:47 -0400321 if (localEndpoint.address().is_multicast()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200322 BOOST_THROW_EXCEPTION(Error("createChannel is only for unicast channels. The provided endpoint "
323 "is multicast. Use createMulticastFace to create a multicast face"));
324 }
325
Yukai Tu0a49d342015-09-13 12:54:22 +0800326 // check if the endpoint is already used by a multicast face
Davide Pesavento46afec42017-05-28 14:28:47 -0400327 if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700328 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP unicast channel, local "
329 "endpoint is already allocated for a UDP multicast face"));
Yukai Tu0a49d342015-09-13 12:54:22 +0800330 }
Junxiao Shi79494162014-04-02 18:25:11 -0700331
Davide Pesavento46afec42017-05-28 14:28:47 -0400332 auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout);
333 m_channels[localEndpoint] = channel;
334 prohibitEndpoint(localEndpoint);
335
Giulio Grassi624f6c62014-02-18 19:42:14 +0100336 return channel;
337}
338
339shared_ptr<UdpChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200340UdpFactory::createChannel(const std::string& localIp, const std::string& localPort,
Davide Pesavento46afec42017-05-28 14:28:47 -0400341 time::nanoseconds idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100342{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200343 udp::Endpoint endpoint(ip::address::from_string(localIp),
344 boost::lexical_cast<uint16_t>(localPort));
Davide Pesavento46afec42017-05-28 14:28:47 -0400345 return createChannel(endpoint, idleTimeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100346}
347
Junxiao Shi64d99f22017-01-21 23:06:36 +0000348std::vector<shared_ptr<const Channel>>
349UdpFactory::getChannels() const
350{
351 return getChannelsFromMap(m_channels);
352}
353
Junxiao Shicde37ad2015-12-24 01:02:05 -0700354shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100355UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200356 const udp::Endpoint& multicastEndpoint,
Davide Pesavento46afec42017-05-28 14:28:47 -0400357 const std::string& networkInterfaceName)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100358{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100359 // checking if the local and multicast endpoints are already in use for a multicast face
Davide Pesavento46afec42017-05-28 14:28:47 -0400360 auto it = m_mcastFaces.find(localEndpoint);
361 if (it != m_mcastFaces.end()) {
362 if (it->second->getRemoteUri() == FaceUri(multicastEndpoint))
363 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100364 else
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700365 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local "
366 "endpoint is already allocated for a UDP multicast face "
367 "on a different multicast group"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100368 }
Junxiao Shi79494162014-04-02 18:25:11 -0700369
Davide Pesavento292e5e12015-03-13 02:08:33 +0100370 // checking if the local endpoint is already in use for a unicast channel
Davide Pesavento46afec42017-05-28 14:28:47 -0400371 if (m_channels.find(localEndpoint) != m_channels.end()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700372 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local "
373 "endpoint is already allocated for a UDP unicast channel"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100374 }
375
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600376 if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700377 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
378 "remote endpoint is owned by this NFD instance"));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600379 }
380
Giulio Grassi624f6c62014-02-18 19:42:14 +0100381 if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700382 BOOST_THROW_EXCEPTION(Error("IPv6 multicast is not supported yet. Please provide an IPv4 "
383 "address"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100384 }
Junxiao Shi79494162014-04-02 18:25:11 -0700385
Giulio Grassi624f6c62014-02-18 19:42:14 +0100386 if (localEndpoint.port() != multicastEndpoint.port()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700387 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
388 "both endpoints should have the same port number. "));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100389 }
390
391 if (!multicastEndpoint.address().is_multicast()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700392 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
393 "the multicast group given as input is not a multicast address"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100394 }
395
Davide Pesavento292e5e12015-03-13 02:08:33 +0100396 ip::udp::socket receiveSocket(getGlobalIoService());
397 receiveSocket.open(multicastEndpoint.protocol());
398 receiveSocket.set_option(ip::udp::socket::reuse_address(true));
399 receiveSocket.bind(multicastEndpoint);
Junxiao Shi79494162014-04-02 18:25:11 -0700400
Davide Pesavento292e5e12015-03-13 02:08:33 +0100401 ip::udp::socket sendSocket(getGlobalIoService());
402 sendSocket.open(multicastEndpoint.protocol());
403 sendSocket.set_option(ip::udp::socket::reuse_address(true));
404 sendSocket.set_option(ip::multicast::enable_loopback(false));
405 sendSocket.bind(udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port()));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200406 if (localEndpoint.address() != ip::address_v4::any())
Davide Pesavento292e5e12015-03-13 02:08:33 +0100407 sendSocket.set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100408
Davide Pesavento292e5e12015-03-13 02:08:33 +0100409 sendSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
410 localEndpoint.address().to_v4()));
411 receiveSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700412 localEndpoint.address().to_v4()));
413
Davide Pesavento292e5e12015-03-13 02:08:33 +0100414#ifdef __linux__
415 /*
416 * On Linux, if there is more than one MulticastUdpFace for the same multicast
417 * group but they are bound to different network interfaces, the socket needs
418 * to be bound to the specific interface using SO_BINDTODEVICE, otherwise the
419 * face will receive all packets sent to the other interfaces as well.
420 * This happens only on Linux. On OS X, the ip::multicast::join_group option
421 * is enough to get the desired behaviour.
422 */
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200423 if (!networkInterfaceName.empty()) {
Davide Pesavento292e5e12015-03-13 02:08:33 +0100424 if (::setsockopt(receiveSocket.native_handle(), SOL_SOCKET, SO_BINDTODEVICE,
425 networkInterfaceName.c_str(), networkInterfaceName.size() + 1) < 0) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700426 BOOST_THROW_EXCEPTION(Error("Cannot bind multicast face to " + networkInterfaceName +
427 ": " + std::strerror(errno)));
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200428 }
429 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000430#endif // __linux__
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200431
Junxiao Shi64d99f22017-01-21 23:06:36 +0000432 auto linkService = make_unique<GenericLinkService>();
433 auto transport = make_unique<MulticastUdpTransport>(localEndpoint, multicastEndpoint,
434 std::move(receiveSocket),
Teng Liangfe4fce32017-03-29 04:49:38 +0000435 std::move(sendSocket),
436 m_mcastConfig.linkType);
Davide Pesavento46afec42017-05-28 14:28:47 -0400437 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700438
Junxiao Shi64d99f22017-01-21 23:06:36 +0000439 m_mcastFaces[localEndpoint] = face;
440 connectFaceClosedSignal(*face, [this, localEndpoint] { m_mcastFaces.erase(localEndpoint); });
Giulio Grassi624f6c62014-02-18 19:42:14 +0100441
Davide Pesavento292e5e12015-03-13 02:08:33 +0100442 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100443}
444
Junxiao Shicde37ad2015-12-24 01:02:05 -0700445shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100446UdpFactory::createMulticastFace(const std::string& localIp,
447 const std::string& multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200448 const std::string& multicastPort,
Davide Pesavento46afec42017-05-28 14:28:47 -0400449 const std::string& networkInterfaceName)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100450{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100451 udp::Endpoint localEndpoint(ip::address::from_string(localIp),
Chengyu Fan4381fb62015-01-14 11:37:04 -0700452 boost::lexical_cast<uint16_t>(multicastPort));
Davide Pesavento292e5e12015-03-13 02:08:33 +0100453 udp::Endpoint multicastEndpoint(ip::address::from_string(multicastIp),
Chengyu Fan4381fb62015-01-14 11:37:04 -0700454 boost::lexical_cast<uint16_t>(multicastPort));
Chengyu Fan4381fb62015-01-14 11:37:04 -0700455 return createMulticastFace(localEndpoint, multicastEndpoint, networkInterfaceName);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100456}
457
Junxiao Shi64d99f22017-01-21 23:06:36 +0000458void
459UdpFactory::applyMulticastConfig(const FaceSystem::ConfigContext& context)
460{
461 // collect old faces
462 std::set<shared_ptr<Face>> oldFaces;
463 boost::copy(m_mcastFaces | boost::adaptors::map_values,
464 std::inserter(oldFaces, oldFaces.end()));
465
466 if (m_mcastConfig.isEnabled) {
467 // determine interfaces on which faces should be created or retained
468 auto capableNetifRange = context.listNetifs() |
469 boost::adaptors::filtered([this] (const NetworkInterfaceInfo& netif) {
470 return netif.isUp() && netif.isMulticastCapable() &&
Junxiao Shic31080d2017-01-24 15:10:12 +0000471 !netif.ipv4Addresses.empty() &&
472 m_mcastConfig.netifPredicate(netif);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000473 });
474
475 bool needIfname = false;
476#ifdef __linux__
477 std::vector<NetworkInterfaceInfo> capableNetifs;
478 boost::copy(capableNetifRange, std::back_inserter(capableNetifs));
479 // on Linux, ifname is needed to create more than one UDP multicast face on the same group
480 needIfname = capableNetifs.size() > 1;
481#else
482 auto& capableNetifs = capableNetifRange;
483#endif // __linux__
484
485 // create faces
486 for (const auto& netif : capableNetifs) {
487 udp::Endpoint localEndpoint(netif.ipv4Addresses.front(), m_mcastConfig.group.port());
488 shared_ptr<Face> face = this->createMulticastFace(localEndpoint, m_mcastConfig.group,
489 needIfname ? netif.name : "");
490 if (face->getId() == INVALID_FACEID) {
491 // new face: register with forwarding
Junxiao Shi2d491752017-07-14 21:32:05 +0000492 this->addFace(face);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000493 }
494 else {
495 // existing face: don't destroy
496 oldFaces.erase(face);
497 }
498 }
499 }
500
501 // destroy old faces that are not needed in new configuration
502 for (const auto& face : oldFaces) {
503 face->close();
504 }
505}
506
507} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100508} // namespace nfd