Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 4 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 24 | */ |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 25 | |
| 26 | #include "udp-factory.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 27 | #include "generic-link-service.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 28 | #include "multicast-udp-transport.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 29 | #include "core/global-io.hpp" |
Yanbiao Li | afb2059 | 2017-08-03 16:20:46 -0700 | [diff] [blame^] | 30 | |
| 31 | #include <ndn-cxx/net/address-converter.hpp> |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 32 | #include <boost/range/adaptors.hpp> |
| 33 | #include <boost/range/algorithm/copy.hpp> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 34 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 35 | #ifdef __linux__ |
| 36 | #include <cerrno> // for errno |
| 37 | #include <cstring> // for std::strerror() |
| 38 | #include <sys/socket.h> // for setsockopt() |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 39 | #endif // __linux__ |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 40 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 41 | namespace nfd { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 42 | namespace face { |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 43 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 44 | namespace ip = boost::asio::ip; |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 45 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 46 | NFD_LOG_INIT("UdpFactory"); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 47 | NFD_REGISTER_PROTOCOL_FACTORY(UdpFactory); |
| 48 | |
| 49 | const std::string& |
| 50 | UdpFactory::getId() |
| 51 | { |
| 52 | static std::string id("udp"); |
| 53 | return id; |
| 54 | } |
| 55 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 56 | UdpFactory::UdpFactory(const CtorParams& params) |
| 57 | : ProtocolFactory(params) |
| 58 | { |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 59 | m_netifAddConn = netmon->onInterfaceAdded.connect(bind(&UdpFactory::applyMcastConfigToNetif, this, _1)); |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 60 | } |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 61 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 62 | void |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 63 | UdpFactory::processConfig(OptionalConfigSection configSection, |
| 64 | FaceSystem::ConfigContext& context) |
| 65 | { |
| 66 | // udp |
| 67 | // { |
| 68 | // port 6363 |
| 69 | // enable_v4 yes |
| 70 | // enable_v6 yes |
| 71 | // idle_timeout 600 |
| 72 | // keep_alive_interval 25 ; acceptable but ignored |
| 73 | // mcast yes |
| 74 | // mcast_group 224.0.23.170 |
| 75 | // mcast_port 56363 |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 76 | // mcast_ad_hoc no |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 77 | // whitelist |
| 78 | // { |
| 79 | // * |
| 80 | // } |
| 81 | // blacklist |
| 82 | // { |
| 83 | // } |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 84 | // } |
| 85 | |
| 86 | uint16_t port = 6363; |
| 87 | bool enableV4 = false; |
| 88 | bool enableV6 = false; |
| 89 | uint32_t idleTimeout = 600; |
| 90 | MulticastConfig mcastConfig; |
| 91 | |
| 92 | if (configSection) { |
| 93 | // These default to 'yes' but only if face_system.udp section is present |
| 94 | enableV4 = enableV6 = mcastConfig.isEnabled = true; |
| 95 | |
| 96 | for (const auto& pair : *configSection) { |
| 97 | const std::string& key = pair.first; |
| 98 | const ConfigSection& value = pair.second; |
| 99 | |
| 100 | if (key == "port") { |
| 101 | port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"); |
| 102 | } |
| 103 | else if (key == "enable_v4") { |
| 104 | enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 105 | } |
| 106 | else if (key == "enable_v6") { |
| 107 | enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 108 | } |
| 109 | else if (key == "idle_timeout") { |
| 110 | idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp"); |
| 111 | } |
| 112 | else if (key == "keep_alive_interval") { |
| 113 | // ignored |
| 114 | } |
| 115 | else if (key == "mcast") { |
| 116 | mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 117 | } |
| 118 | else if (key == "mcast_group") { |
| 119 | const std::string& valueStr = value.get_value<std::string>(); |
| 120 | boost::system::error_code ec; |
| 121 | mcastConfig.group.address(boost::asio::ip::address_v4::from_string(valueStr, ec)); |
| 122 | if (ec) { |
| 123 | BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" + |
| 124 | valueStr + "' cannot be parsed as an IPv4 address")); |
| 125 | } |
| 126 | else if (!mcastConfig.group.address().is_multicast()) { |
| 127 | BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" + |
| 128 | valueStr + "' is not a multicast address")); |
| 129 | } |
| 130 | } |
| 131 | else if (key == "mcast_port") { |
| 132 | mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp")); |
| 133 | } |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 134 | else if (key == "mcast_ad_hoc") { |
| 135 | bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 136 | mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS; |
| 137 | } |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 138 | else if (key == "whitelist") { |
| 139 | mcastConfig.netifPredicate.parseWhitelist(value); |
| 140 | } |
| 141 | else if (key == "blacklist") { |
| 142 | mcastConfig.netifPredicate.parseBlacklist(value); |
| 143 | } |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 144 | else { |
| 145 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.udp." + key)); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) { |
| 150 | BOOST_THROW_EXCEPTION(ConfigFile::Error( |
| 151 | "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. " |
| 152 | "Remove face_system.udp section to disable UDP channels or enable at least one of them.")); |
| 153 | } |
| 154 | } |
| 155 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 156 | if (context.isDryRun) { |
| 157 | return; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 158 | } |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 159 | |
| 160 | if (enableV4) { |
| 161 | udp::Endpoint endpoint(ip::udp::v4(), port); |
| 162 | shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout)); |
| 163 | if (!v4Channel->isListening()) { |
| 164 | v4Channel->listen(this->addFace, nullptr); |
| 165 | } |
| 166 | providedSchemes.insert("udp"); |
| 167 | providedSchemes.insert("udp4"); |
| 168 | } |
| 169 | else if (providedSchemes.count("udp4") > 0) { |
| 170 | NFD_LOG_WARN("Cannot close udp4 channel after its creation"); |
| 171 | } |
| 172 | |
| 173 | if (enableV6) { |
| 174 | udp::Endpoint endpoint(ip::udp::v6(), port); |
| 175 | shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout)); |
| 176 | if (!v6Channel->isListening()) { |
| 177 | v6Channel->listen(this->addFace, nullptr); |
| 178 | } |
| 179 | providedSchemes.insert("udp"); |
| 180 | providedSchemes.insert("udp6"); |
| 181 | } |
| 182 | else if (providedSchemes.count("udp6") > 0) { |
| 183 | NFD_LOG_WARN("Cannot close udp6 channel after its creation"); |
| 184 | } |
| 185 | |
| 186 | if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) { |
| 187 | if (mcastConfig.isEnabled) { |
| 188 | NFD_LOG_INFO("enabling multicast on " << mcastConfig.group); |
| 189 | } |
| 190 | else { |
| 191 | NFD_LOG_INFO("disabling multicast"); |
| 192 | } |
| 193 | } |
| 194 | else if (mcastConfig.isEnabled) { |
| 195 | if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) { |
| 196 | NFD_LOG_WARN("Cannot change ad hoc setting on existing faces"); |
| 197 | } |
| 198 | if (m_mcastConfig.group != mcastConfig.group) { |
| 199 | NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group << |
| 200 | " to " << mcastConfig.group); |
| 201 | } |
| 202 | if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) { |
| 203 | NFD_LOG_INFO("changing whitelist/blacklist"); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Even if there's no configuration change, we still need to re-apply configuration because |
| 208 | // netifs may have changed. |
| 209 | m_mcastConfig = mcastConfig; |
| 210 | this->applyMcastConfig(context); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void |
Eric Newberry | 944f38b | 2017-07-20 20:54:22 -0400 | [diff] [blame] | 214 | UdpFactory::createFace(const CreateFaceParams& params, |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 215 | const FaceCreatedCallback& onCreated, |
| 216 | const FaceCreationFailedCallback& onFailure) |
| 217 | { |
Eric Newberry | 944f38b | 2017-07-20 20:54:22 -0400 | [diff] [blame] | 218 | BOOST_ASSERT(params.remoteUri.isCanonical()); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 219 | |
Eric Newberry | 944f38b | 2017-07-20 20:54:22 -0400 | [diff] [blame] | 220 | if (params.localUri) { |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 221 | NFD_LOG_TRACE("Cannot create unicast UDP face with LocalUri"); |
| 222 | onFailure(406, "Unicast UDP faces cannot be created with a LocalUri"); |
| 223 | return; |
| 224 | } |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 225 | |
Eric Newberry | 944f38b | 2017-07-20 20:54:22 -0400 | [diff] [blame] | 226 | if (params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 227 | NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND"); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 228 | onFailure(406, "Outgoing UDP faces do not support on-demand persistency"); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 229 | return; |
| 230 | } |
| 231 | |
Yanbiao Li | afb2059 | 2017-08-03 16:20:46 -0700 | [diff] [blame^] | 232 | udp::Endpoint endpoint(ndn::ip::addressFromString(params.remoteUri.getHost()), |
Eric Newberry | 944f38b | 2017-07-20 20:54:22 -0400 | [diff] [blame] | 233 | boost::lexical_cast<uint16_t>(params.remoteUri.getPort())); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 234 | |
| 235 | if (endpoint.address().is_multicast()) { |
| 236 | NFD_LOG_TRACE("createFace does not support multicast faces"); |
| 237 | onFailure(406, "Cannot create multicast UDP faces"); |
| 238 | return; |
| 239 | } |
| 240 | |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 241 | if (params.wantLocalFields) { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 242 | // UDP faces are never local |
| 243 | NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled"); |
| 244 | onFailure(406, "Local fields can only be enabled on faces with local scope"); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | // very simple logic for now |
| 249 | for (const auto& i : m_channels) { |
| 250 | if ((i.first.address().is_v4() && endpoint.address().is_v4()) || |
| 251 | (i.first.address().is_v6() && endpoint.address().is_v6())) { |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 252 | i.second->connect(endpoint, params.persistency, params.wantLpReliability, |
| 253 | onCreated, onFailure); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 254 | return; |
| 255 | } |
| 256 | } |
| 257 | |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 258 | NFD_LOG_TRACE("No channels available to connect to " << endpoint); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 259 | onFailure(504, "No channels available to connect"); |
| 260 | } |
| 261 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 262 | shared_ptr<UdpChannel> |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 263 | UdpFactory::createChannel(const udp::Endpoint& localEndpoint, |
| 264 | time::nanoseconds idleTimeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 265 | { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 266 | auto it = m_channels.find(localEndpoint); |
| 267 | if (it != m_channels.end()) |
| 268 | return it->second; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 269 | |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 270 | if (localEndpoint.address().is_multicast()) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 271 | BOOST_THROW_EXCEPTION(Error("createChannel is only for unicast channels. The provided endpoint " |
| 272 | "is multicast. Use createMulticastFace to create a multicast face")); |
| 273 | } |
| 274 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 275 | // check if the endpoint is already used by a multicast face |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 276 | if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 277 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP unicast channel, local " |
| 278 | "endpoint is already allocated for a UDP multicast face")); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 279 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 280 | |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 281 | auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout); |
| 282 | m_channels[localEndpoint] = channel; |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 283 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 284 | return channel; |
| 285 | } |
| 286 | |
| 287 | shared_ptr<UdpChannel> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 288 | UdpFactory::createChannel(const std::string& localIp, const std::string& localPort, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 289 | time::nanoseconds idleTimeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 290 | { |
Yanbiao Li | afb2059 | 2017-08-03 16:20:46 -0700 | [diff] [blame^] | 291 | udp::Endpoint endpoint(ndn::ip::addressFromString(localIp), |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 292 | boost::lexical_cast<uint16_t>(localPort)); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 293 | return createChannel(endpoint, idleTimeout); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 296 | std::vector<shared_ptr<const Channel>> |
| 297 | UdpFactory::getChannels() const |
| 298 | { |
| 299 | return getChannelsFromMap(m_channels); |
| 300 | } |
| 301 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 302 | shared_ptr<Face> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 303 | UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint, |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 304 | const udp::Endpoint& multicastEndpoint, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 305 | const std::string& networkInterfaceName) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 306 | { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 307 | // checking if the local and multicast endpoints are already in use for a multicast face |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 308 | auto it = m_mcastFaces.find(localEndpoint); |
| 309 | if (it != m_mcastFaces.end()) { |
| 310 | if (it->second->getRemoteUri() == FaceUri(multicastEndpoint)) |
| 311 | return it->second; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 312 | else |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 313 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local " |
| 314 | "endpoint is already allocated for a UDP multicast face " |
| 315 | "on a different multicast group")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 316 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 317 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 318 | // checking if the local endpoint is already in use for a unicast channel |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 319 | if (m_channels.find(localEndpoint) != m_channels.end()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 320 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local " |
| 321 | "endpoint is already allocated for a UDP unicast channel")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 325 | BOOST_THROW_EXCEPTION(Error("IPv6 multicast is not supported yet. Please provide an IPv4 " |
| 326 | "address")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 327 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 328 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 329 | if (localEndpoint.port() != multicastEndpoint.port()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 330 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, " |
| 331 | "both endpoints should have the same port number. ")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | if (!multicastEndpoint.address().is_multicast()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 335 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, " |
| 336 | "the multicast group given as input is not a multicast address")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 339 | ip::udp::socket receiveSocket(getGlobalIoService()); |
| 340 | receiveSocket.open(multicastEndpoint.protocol()); |
| 341 | receiveSocket.set_option(ip::udp::socket::reuse_address(true)); |
| 342 | receiveSocket.bind(multicastEndpoint); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 343 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 344 | ip::udp::socket sendSocket(getGlobalIoService()); |
| 345 | sendSocket.open(multicastEndpoint.protocol()); |
| 346 | sendSocket.set_option(ip::udp::socket::reuse_address(true)); |
| 347 | sendSocket.set_option(ip::multicast::enable_loopback(false)); |
| 348 | sendSocket.bind(udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port())); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 349 | if (localEndpoint.address() != ip::address_v4::any()) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 350 | sendSocket.set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4())); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 351 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 352 | sendSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(), |
| 353 | localEndpoint.address().to_v4())); |
| 354 | receiveSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(), |
Alexander Afanasyev | dc01700 | 2014-06-18 16:37:54 -0700 | [diff] [blame] | 355 | localEndpoint.address().to_v4())); |
| 356 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 357 | #ifdef __linux__ |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 358 | // On Linux, if there is more than one multicast UDP face for the same multicast |
| 359 | // group but they are bound to different network interfaces, the socket needs |
| 360 | // to be bound to the specific interface using SO_BINDTODEVICE, otherwise the |
| 361 | // face will receive all packets sent to the other interfaces as well. |
| 362 | // This happens only on Linux. On macOS, the ip::multicast::join_group option |
| 363 | // is enough to get the desired behaviour. |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 364 | if (!networkInterfaceName.empty()) { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 365 | if (::setsockopt(receiveSocket.native_handle(), SOL_SOCKET, SO_BINDTODEVICE, |
| 366 | networkInterfaceName.c_str(), networkInterfaceName.size() + 1) < 0) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 367 | BOOST_THROW_EXCEPTION(Error("Cannot bind multicast face to " + networkInterfaceName + |
| 368 | ": " + std::strerror(errno))); |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 369 | } |
| 370 | } |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 371 | #endif // __linux__ |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 372 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 373 | auto linkService = make_unique<GenericLinkService>(); |
| 374 | auto transport = make_unique<MulticastUdpTransport>(localEndpoint, multicastEndpoint, |
| 375 | std::move(receiveSocket), |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 376 | std::move(sendSocket), |
| 377 | m_mcastConfig.linkType); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 378 | auto face = make_shared<Face>(std::move(linkService), std::move(transport)); |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 379 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 380 | m_mcastFaces[localEndpoint] = face; |
| 381 | connectFaceClosedSignal(*face, [this, localEndpoint] { m_mcastFaces.erase(localEndpoint); }); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 382 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 383 | return face; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 386 | shared_ptr<Face> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 387 | UdpFactory::createMulticastFace(const std::string& localIp, |
| 388 | const std::string& multicastIp, |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 389 | const std::string& multicastPort, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 390 | const std::string& networkInterfaceName) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 391 | { |
Yanbiao Li | afb2059 | 2017-08-03 16:20:46 -0700 | [diff] [blame^] | 392 | udp::Endpoint localEndpoint(ndn::ip::addressFromString(localIp), |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 393 | boost::lexical_cast<uint16_t>(multicastPort)); |
Yanbiao Li | afb2059 | 2017-08-03 16:20:46 -0700 | [diff] [blame^] | 394 | udp::Endpoint multicastEndpoint(ndn::ip::addressFromString(multicastIp), |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 395 | boost::lexical_cast<uint16_t>(multicastPort)); |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 396 | return createMulticastFace(localEndpoint, multicastEndpoint, networkInterfaceName); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 397 | } |
| 398 | |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 399 | static ndn::optional<ndn::net::NetworkAddress> |
| 400 | getV4Address(const ndn::net::NetworkInterface& netif) |
| 401 | { |
| 402 | using namespace ndn::net; |
| 403 | for (const NetworkAddress& na : netif.getNetworkAddresses()) { |
| 404 | if (na.getFamily() == AddressFamily::V4 && na.getScope() != AddressScope::NOWHERE) { |
| 405 | return na; |
| 406 | } |
| 407 | } |
| 408 | return ndn::nullopt; |
| 409 | } |
| 410 | |
| 411 | shared_ptr<Face> |
| 412 | UdpFactory::applyMcastConfigToNetif(const shared_ptr<const ndn::net::NetworkInterface>& netif) |
| 413 | { |
| 414 | if (!m_mcastConfig.isEnabled) { |
| 415 | return nullptr; |
| 416 | } |
| 417 | |
| 418 | if (!netif->isUp()) { |
| 419 | NFD_LOG_DEBUG("Not creating multicast face on " << netif->getName() << ": netif is down"); |
| 420 | return nullptr; |
| 421 | } |
| 422 | |
| 423 | if (!netif->canMulticast()) { |
| 424 | NFD_LOG_DEBUG("Not creating multicast face on " << netif->getName() << ": netif cannot multicast"); |
| 425 | return nullptr; |
| 426 | } |
| 427 | |
| 428 | auto address = getV4Address(*netif); |
| 429 | if (!address) { |
| 430 | NFD_LOG_DEBUG("Not creating multicast face on " << netif->getName() << ": no IPv4 address"); |
| 431 | // keep an eye on new addresses |
| 432 | m_netifConns[netif->getIndex()].addrAddConn = |
| 433 | netif->onAddressAdded.connectSingleShot(bind(&UdpFactory::applyMcastConfigToNetif, this, netif)); |
| 434 | return nullptr; |
| 435 | } |
| 436 | |
| 437 | if (!m_mcastConfig.netifPredicate(*netif)) { |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 438 | NFD_LOG_DEBUG("Not creating multicast face on " << netif->getName() << ": rejected by whitelist/blacklist"); |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 439 | return nullptr; |
| 440 | } |
| 441 | |
| 442 | NFD_LOG_DEBUG("Creating multicast face on " << netif->getName()); |
| 443 | udp::Endpoint localEndpoint(address->getIp(), m_mcastConfig.group.port()); |
| 444 | auto face = this->createMulticastFace(localEndpoint, m_mcastConfig.group, netif->getName()); |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 445 | // ifname is only used on Linux. It is not required if there is only one multicast-capable netif, |
| 446 | // but it is always supplied because a new netif can be added at anytime. |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 447 | |
| 448 | if (face->getId() == INVALID_FACEID) { |
| 449 | // new face: register with forwarding |
| 450 | this->addFace(face); |
| 451 | } |
| 452 | return face; |
| 453 | } |
| 454 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 455 | void |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 456 | UdpFactory::applyMcastConfig(const FaceSystem::ConfigContext& context) |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 457 | { |
| 458 | // collect old faces |
| 459 | std::set<shared_ptr<Face>> oldFaces; |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 460 | boost::copy(m_mcastFaces | boost::adaptors::map_values, std::inserter(oldFaces, oldFaces.end())); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 461 | |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 462 | // create faces if requested by config |
| 463 | for (const auto& netif : netmon->listNetworkInterfaces()) { |
| 464 | auto face = this->applyMcastConfigToNetif(netif); |
| 465 | if (face != nullptr) { |
| 466 | // don't destroy face |
| 467 | oldFaces.erase(face); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
| 471 | // destroy old faces that are not needed in new configuration |
| 472 | for (const auto& face : oldFaces) { |
| 473 | face->close(); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | } // namespace face |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 478 | } // namespace nfd |