blob: a544edf3616fdb8172ae310f397519a1c1113030 [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");
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
Giulio Grassi624f6c62014-02-18 19:42:14 +010054
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060055void
Junxiao Shi64d99f22017-01-21 23:06:36 +000056UdpFactory::processConfig(OptionalConfigSection configSection,
57 FaceSystem::ConfigContext& context)
58{
59 // udp
60 // {
61 // port 6363
62 // enable_v4 yes
63 // enable_v6 yes
64 // idle_timeout 600
65 // keep_alive_interval 25 ; acceptable but ignored
66 // mcast yes
67 // mcast_group 224.0.23.170
68 // mcast_port 56363
Teng Liangfe4fce32017-03-29 04:49:38 +000069 // mcast_ad_hoc no
Junxiao Shic31080d2017-01-24 15:10:12 +000070 // whitelist
71 // {
72 // *
73 // }
74 // blacklist
75 // {
76 // }
Junxiao Shi64d99f22017-01-21 23:06:36 +000077 // }
78
79 uint16_t port = 6363;
80 bool enableV4 = false;
81 bool enableV6 = false;
82 uint32_t idleTimeout = 600;
83 MulticastConfig mcastConfig;
84
85 if (configSection) {
86 // These default to 'yes' but only if face_system.udp section is present
87 enableV4 = enableV6 = mcastConfig.isEnabled = true;
88
89 for (const auto& pair : *configSection) {
90 const std::string& key = pair.first;
91 const ConfigSection& value = pair.second;
92
93 if (key == "port") {
94 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp");
95 }
96 else if (key == "enable_v4") {
97 enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp");
98 }
99 else if (key == "enable_v6") {
100 enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp");
101 }
102 else if (key == "idle_timeout") {
103 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp");
104 }
105 else if (key == "keep_alive_interval") {
106 // ignored
107 }
108 else if (key == "mcast") {
109 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp");
110 }
111 else if (key == "mcast_group") {
112 const std::string& valueStr = value.get_value<std::string>();
113 boost::system::error_code ec;
114 mcastConfig.group.address(boost::asio::ip::address_v4::from_string(valueStr, ec));
115 if (ec) {
116 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
117 valueStr + "' cannot be parsed as an IPv4 address"));
118 }
119 else if (!mcastConfig.group.address().is_multicast()) {
120 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" +
121 valueStr + "' is not a multicast address"));
122 }
123 }
124 else if (key == "mcast_port") {
125 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
126 }
Teng Liangfe4fce32017-03-29 04:49:38 +0000127 else if (key == "mcast_ad_hoc") {
128 bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.udp");
129 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
130 }
Junxiao Shic31080d2017-01-24 15:10:12 +0000131 else if (key == "whitelist") {
132 mcastConfig.netifPredicate.parseWhitelist(value);
133 }
134 else if (key == "blacklist") {
135 mcastConfig.netifPredicate.parseBlacklist(value);
136 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000137 else {
138 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.udp." + key));
139 }
140 }
141
142 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
143 BOOST_THROW_EXCEPTION(ConfigFile::Error(
144 "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. "
145 "Remove face_system.udp section to disable UDP channels or enable at least one of them."));
146 }
147 }
148
149 if (!context.isDryRun) {
150 if (enableV4) {
151 udp::Endpoint endpoint(ip::udp::v4(), port);
152 shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
153 if (!v4Channel->isListening()) {
154 v4Channel->listen(context.addFace, nullptr);
155 }
156 providedSchemes.insert("udp");
157 providedSchemes.insert("udp4");
158 }
159 else if (providedSchemes.count("udp4") > 0) {
160 NFD_LOG_WARN("Cannot close udp4 channel after its creation");
161 }
162
163 if (enableV6) {
164 udp::Endpoint endpoint(ip::udp::v6(), port);
165 shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
166 if (!v6Channel->isListening()) {
167 v6Channel->listen(context.addFace, nullptr);
168 }
169 providedSchemes.insert("udp");
170 providedSchemes.insert("udp6");
171 }
172 else if (providedSchemes.count("udp6") > 0) {
173 NFD_LOG_WARN("Cannot close udp6 channel after its creation");
174 }
175
176 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
177 if (mcastConfig.isEnabled) {
178 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
179 }
180 else {
181 NFD_LOG_INFO("disabling multicast");
182 }
183 }
Teng Liangfe4fce32017-03-29 04:49:38 +0000184 else if (mcastConfig.isEnabled) {
185 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
186 NFD_LOG_WARN("Cannot change ad hoc setting on existing faces");
187 }
188 if (m_mcastConfig.group != mcastConfig.group) {
189 NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group <<
190 " to " << mcastConfig.group);
191 }
192 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
193 NFD_LOG_INFO("changing whitelist/blacklist");
194 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000195 }
196
Teng Liangfe4fce32017-03-29 04:49:38 +0000197 // Even if there's no configuration change, we still need to re-apply configuration because
198 // netifs may have changed.
Junxiao Shi64d99f22017-01-21 23:06:36 +0000199 m_mcastConfig = mcastConfig;
200 this->applyMulticastConfig(context);
201 }
202}
203
204void
Eric Newberry78e32b02017-04-01 14:34:44 +0000205UdpFactory::createFace(const FaceUri& remoteUri,
206 const ndn::optional<FaceUri>& localUri,
Junxiao Shi64d99f22017-01-21 23:06:36 +0000207 ndn::nfd::FacePersistency persistency,
208 bool wantLocalFieldsEnabled,
209 const FaceCreatedCallback& onCreated,
210 const FaceCreationFailedCallback& onFailure)
211{
Eric Newberry78e32b02017-04-01 14:34:44 +0000212 BOOST_ASSERT(remoteUri.isCanonical());
213
214 if (localUri) {
215 NFD_LOG_TRACE("Cannot create unicast UDP face with LocalUri");
216 onFailure(406, "Unicast UDP faces cannot be created with a LocalUri");
217 return;
218 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000219
220 if (persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
221 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
222 onFailure(406, "Outgoing unicast UDP faces do not support on-demand persistency");
223 return;
224 }
225
Eric Newberry78e32b02017-04-01 14:34:44 +0000226 udp::Endpoint endpoint(ip::address::from_string(remoteUri.getHost()),
227 boost::lexical_cast<uint16_t>(remoteUri.getPort()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000228
229 if (endpoint.address().is_multicast()) {
230 NFD_LOG_TRACE("createFace does not support multicast faces");
231 onFailure(406, "Cannot create multicast UDP faces");
232 return;
233 }
234
235 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
236 NFD_LOG_TRACE("Requested endpoint is prohibited "
237 "(reserved by this NFD or disallowed by face management protocol)");
238 onFailure(406, "Requested endpoint is prohibited");
239 return;
240 }
241
242 if (wantLocalFieldsEnabled) {
243 // UDP faces are never local
244 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
245 onFailure(406, "Local fields can only be enabled on faces with local scope");
246 return;
247 }
248
249 // very simple logic for now
250 for (const auto& i : m_channels) {
251 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
252 (i.first.address().is_v6() && endpoint.address().is_v6())) {
253 i.second->connect(endpoint, persistency, onCreated, onFailure);
254 return;
255 }
256 }
257
258 NFD_LOG_TRACE("No channels available to connect to " + boost::lexical_cast<std::string>(endpoint));
259 onFailure(504, "No channels available to connect");
260}
261
262void
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600263UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint)
264{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200265 if (endpoint.address().is_v4() &&
266 endpoint.address() == ip::address_v4::any()) {
267 prohibitAllIpv4Endpoints(endpoint.port());
268 }
269 else if (endpoint.address().is_v6() &&
270 endpoint.address() == ip::address_v6::any()) {
271 prohibitAllIpv6Endpoints(endpoint.port());
272 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600273
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800274 NFD_LOG_TRACE("prohibiting UDP " << endpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600275 m_prohibitedEndpoints.insert(endpoint);
276}
277
278void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200279UdpFactory::prohibitAllIpv4Endpoints(uint16_t port)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600280{
Davide Pesaventob499a602014-11-18 22:36:56 +0100281 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200282 for (const auto& addr : nic.ipv4Addresses) {
283 if (addr != ip::address_v4::any()) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800284 prohibitEndpoint(udp::Endpoint(addr, port));
285 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600286 }
287
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200288 if (nic.isBroadcastCapable() &&
289 nic.broadcastAddress != ip::address_v4::any()) {
Davide Pesaventob499a602014-11-18 22:36:56 +0100290 prohibitEndpoint(udp::Endpoint(nic.broadcastAddress, port));
291 }
292 }
293
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200294 prohibitEndpoint(udp::Endpoint(ip::address_v4::broadcast(), port));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600295}
296
297void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200298UdpFactory::prohibitAllIpv6Endpoints(uint16_t port)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600299{
Davide Pesaventob499a602014-11-18 22:36:56 +0100300 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200301 for (const auto& addr : nic.ipv6Addresses) {
302 if (addr != ip::address_v6::any()) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800303 prohibitEndpoint(udp::Endpoint(addr, port));
304 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600305 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100306 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600307}
308
Giulio Grassi624f6c62014-02-18 19:42:14 +0100309shared_ptr<UdpChannel>
310UdpFactory::createChannel(const udp::Endpoint& endpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700311 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100312{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200313 auto channel = findChannel(endpoint);
314 if (channel)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100315 return channel;
316
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200317 if (endpoint.address().is_multicast()) {
318 BOOST_THROW_EXCEPTION(Error("createChannel is only for unicast channels. The provided endpoint "
319 "is multicast. Use createMulticastFace to create a multicast face"));
320 }
321
Yukai Tu0a49d342015-09-13 12:54:22 +0800322 // check if the endpoint is already used by a multicast face
323 auto face = findMulticastFace(endpoint);
324 if (face) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700325 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP unicast channel, local "
326 "endpoint is already allocated for a UDP multicast face"));
Yukai Tu0a49d342015-09-13 12:54:22 +0800327 }
Junxiao Shi79494162014-04-02 18:25:11 -0700328
Yumin Xiaab497452016-05-10 20:23:24 +0800329 channel = std::make_shared<UdpChannel>(endpoint, timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100330 m_channels[endpoint] = channel;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600331 prohibitEndpoint(endpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100332 return channel;
333}
334
335shared_ptr<UdpChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200336UdpFactory::createChannel(const std::string& localIp, const std::string& localPort,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700337 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100338{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200339 udp::Endpoint endpoint(ip::address::from_string(localIp),
340 boost::lexical_cast<uint16_t>(localPort));
Alexander Afanasyev0e156df2015-01-26 22:33:43 -0800341 return createChannel(endpoint, timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100342}
343
Junxiao Shi64d99f22017-01-21 23:06:36 +0000344std::vector<shared_ptr<const Channel>>
345UdpFactory::getChannels() const
346{
347 return getChannelsFromMap(m_channels);
348}
349
350shared_ptr<UdpChannel>
351UdpFactory::findChannel(const udp::Endpoint& localEndpoint) const
352{
353 auto i = m_channels.find(localEndpoint);
354 if (i != m_channels.end())
355 return i->second;
356 else
357 return nullptr;
358}
359
Junxiao Shicde37ad2015-12-24 01:02:05 -0700360shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100361UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200362 const udp::Endpoint& multicastEndpoint,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100363 const std::string& networkInterfaceName/* = ""*/)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100364{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100365 // checking if the local and multicast endpoints are already in use for a multicast face
Yukai Tu0a49d342015-09-13 12:54:22 +0800366 auto face = findMulticastFace(localEndpoint);
367 if (face) {
368 if (face->getRemoteUri() == FaceUri(multicastEndpoint))
Davide Pesavento292e5e12015-03-13 02:08:33 +0100369 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100370 else
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700371 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local "
372 "endpoint is already allocated for a UDP multicast face "
373 "on a different multicast group"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100374 }
Junxiao Shi79494162014-04-02 18:25:11 -0700375
Davide Pesavento292e5e12015-03-13 02:08:33 +0100376 // checking if the local endpoint is already in use for a unicast channel
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200377 auto unicastCh = findChannel(localEndpoint);
378 if (unicastCh) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700379 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local "
380 "endpoint is already allocated for a UDP unicast channel"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100381 }
382
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600383 if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700384 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
385 "remote endpoint is owned by this NFD instance"));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600386 }
387
Giulio Grassi624f6c62014-02-18 19:42:14 +0100388 if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700389 BOOST_THROW_EXCEPTION(Error("IPv6 multicast is not supported yet. Please provide an IPv4 "
390 "address"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100391 }
Junxiao Shi79494162014-04-02 18:25:11 -0700392
Giulio Grassi624f6c62014-02-18 19:42:14 +0100393 if (localEndpoint.port() != multicastEndpoint.port()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700394 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
395 "both endpoints should have the same port number. "));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100396 }
397
398 if (!multicastEndpoint.address().is_multicast()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700399 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
400 "the multicast group given as input is not a multicast address"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100401 }
402
Davide Pesavento292e5e12015-03-13 02:08:33 +0100403 ip::udp::socket receiveSocket(getGlobalIoService());
404 receiveSocket.open(multicastEndpoint.protocol());
405 receiveSocket.set_option(ip::udp::socket::reuse_address(true));
406 receiveSocket.bind(multicastEndpoint);
Junxiao Shi79494162014-04-02 18:25:11 -0700407
Davide Pesavento292e5e12015-03-13 02:08:33 +0100408 ip::udp::socket sendSocket(getGlobalIoService());
409 sendSocket.open(multicastEndpoint.protocol());
410 sendSocket.set_option(ip::udp::socket::reuse_address(true));
411 sendSocket.set_option(ip::multicast::enable_loopback(false));
412 sendSocket.bind(udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port()));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200413 if (localEndpoint.address() != ip::address_v4::any())
Davide Pesavento292e5e12015-03-13 02:08:33 +0100414 sendSocket.set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100415
Davide Pesavento292e5e12015-03-13 02:08:33 +0100416 sendSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
417 localEndpoint.address().to_v4()));
418 receiveSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700419 localEndpoint.address().to_v4()));
420
Davide Pesavento292e5e12015-03-13 02:08:33 +0100421#ifdef __linux__
422 /*
423 * On Linux, if there is more than one MulticastUdpFace for the same multicast
424 * group but they are bound to different network interfaces, the socket needs
425 * to be bound to the specific interface using SO_BINDTODEVICE, otherwise the
426 * face will receive all packets sent to the other interfaces as well.
427 * This happens only on Linux. On OS X, the ip::multicast::join_group option
428 * is enough to get the desired behaviour.
429 */
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200430 if (!networkInterfaceName.empty()) {
Davide Pesavento292e5e12015-03-13 02:08:33 +0100431 if (::setsockopt(receiveSocket.native_handle(), SOL_SOCKET, SO_BINDTODEVICE,
432 networkInterfaceName.c_str(), networkInterfaceName.size() + 1) < 0) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700433 BOOST_THROW_EXCEPTION(Error("Cannot bind multicast face to " + networkInterfaceName +
434 ": " + std::strerror(errno)));
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200435 }
436 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000437#endif // __linux__
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200438
Junxiao Shi64d99f22017-01-21 23:06:36 +0000439 auto linkService = make_unique<GenericLinkService>();
440 auto transport = make_unique<MulticastUdpTransport>(localEndpoint, multicastEndpoint,
441 std::move(receiveSocket),
Teng Liangfe4fce32017-03-29 04:49:38 +0000442 std::move(sendSocket),
443 m_mcastConfig.linkType);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700444 face = make_shared<Face>(std::move(linkService), std::move(transport));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700445
Junxiao Shi64d99f22017-01-21 23:06:36 +0000446 m_mcastFaces[localEndpoint] = face;
447 connectFaceClosedSignal(*face, [this, localEndpoint] { m_mcastFaces.erase(localEndpoint); });
Giulio Grassi624f6c62014-02-18 19:42:14 +0100448
Davide Pesavento292e5e12015-03-13 02:08:33 +0100449 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100450}
451
Junxiao Shicde37ad2015-12-24 01:02:05 -0700452shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100453UdpFactory::createMulticastFace(const std::string& localIp,
454 const std::string& multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200455 const std::string& multicastPort,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100456 const std::string& networkInterfaceName/* = ""*/)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100457{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100458 udp::Endpoint localEndpoint(ip::address::from_string(localIp),
Chengyu Fan4381fb62015-01-14 11:37:04 -0700459 boost::lexical_cast<uint16_t>(multicastPort));
Davide Pesavento292e5e12015-03-13 02:08:33 +0100460 udp::Endpoint multicastEndpoint(ip::address::from_string(multicastIp),
Chengyu Fan4381fb62015-01-14 11:37:04 -0700461 boost::lexical_cast<uint16_t>(multicastPort));
Chengyu Fan4381fb62015-01-14 11:37:04 -0700462 return createMulticastFace(localEndpoint, multicastEndpoint, networkInterfaceName);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100463}
464
Junxiao Shicde37ad2015-12-24 01:02:05 -0700465shared_ptr<Face>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200466UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint) const
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600467{
Junxiao Shi64d99f22017-01-21 23:06:36 +0000468 auto i = m_mcastFaces.find(localEndpoint);
469 if (i != m_mcastFaces.end())
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200470 return i->second;
471 else
472 return nullptr;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600473}
474
Junxiao Shi64d99f22017-01-21 23:06:36 +0000475void
476UdpFactory::applyMulticastConfig(const FaceSystem::ConfigContext& context)
477{
478 // collect old faces
479 std::set<shared_ptr<Face>> oldFaces;
480 boost::copy(m_mcastFaces | boost::adaptors::map_values,
481 std::inserter(oldFaces, oldFaces.end()));
482
483 if (m_mcastConfig.isEnabled) {
484 // determine interfaces on which faces should be created or retained
485 auto capableNetifRange = context.listNetifs() |
486 boost::adaptors::filtered([this] (const NetworkInterfaceInfo& netif) {
487 return netif.isUp() && netif.isMulticastCapable() &&
Junxiao Shic31080d2017-01-24 15:10:12 +0000488 !netif.ipv4Addresses.empty() &&
489 m_mcastConfig.netifPredicate(netif);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000490 });
491
492 bool needIfname = false;
493#ifdef __linux__
494 std::vector<NetworkInterfaceInfo> capableNetifs;
495 boost::copy(capableNetifRange, std::back_inserter(capableNetifs));
496 // on Linux, ifname is needed to create more than one UDP multicast face on the same group
497 needIfname = capableNetifs.size() > 1;
498#else
499 auto& capableNetifs = capableNetifRange;
500#endif // __linux__
501
502 // create faces
503 for (const auto& netif : capableNetifs) {
504 udp::Endpoint localEndpoint(netif.ipv4Addresses.front(), m_mcastConfig.group.port());
505 shared_ptr<Face> face = this->createMulticastFace(localEndpoint, m_mcastConfig.group,
506 needIfname ? netif.name : "");
507 if (face->getId() == INVALID_FACEID) {
508 // new face: register with forwarding
509 context.addFace(face);
510 }
511 else {
512 // existing face: don't destroy
513 oldFaces.erase(face);
514 }
515 }
516 }
517
518 // destroy old faces that are not needed in new configuration
519 for (const auto& face : oldFaces) {
520 face->close();
521 }
522}
523
524} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100525} // namespace nfd