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 | /* |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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> |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 32 | #include <boost/range/adaptor/map.hpp> |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 33 | #include <boost/range/algorithm/copy.hpp> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 34 | |
| 35 | namespace nfd { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 36 | namespace face { |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 37 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 38 | namespace ip = boost::asio::ip; |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 39 | namespace net = ndn::net; |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 40 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 41 | NFD_LOG_INIT("UdpFactory"); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 42 | NFD_REGISTER_PROTOCOL_FACTORY(UdpFactory); |
| 43 | |
| 44 | const std::string& |
| 45 | UdpFactory::getId() |
| 46 | { |
| 47 | static std::string id("udp"); |
| 48 | return id; |
| 49 | } |
| 50 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 51 | UdpFactory::UdpFactory(const CtorParams& params) |
| 52 | : ProtocolFactory(params) |
| 53 | { |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 54 | m_netifAddConn = netmon->onInterfaceAdded.connect(bind(&UdpFactory::applyMcastConfigToNetif, this, _1)); |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 55 | } |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 56 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 57 | void |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 58 | UdpFactory::processConfig(OptionalConfigSection configSection, |
| 59 | FaceSystem::ConfigContext& context) |
| 60 | { |
| 61 | // udp |
| 62 | // { |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 63 | // listen yes |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 64 | // port 6363 |
| 65 | // enable_v4 yes |
| 66 | // enable_v6 yes |
| 67 | // idle_timeout 600 |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 68 | // mcast yes |
| 69 | // mcast_group 224.0.23.170 |
| 70 | // mcast_port 56363 |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 71 | // mcast_group_v6 ff02::1234 |
| 72 | // mcast_port_v6 56363 |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 73 | // mcast_ad_hoc no |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 74 | // whitelist |
| 75 | // { |
| 76 | // * |
| 77 | // } |
| 78 | // blacklist |
| 79 | // { |
| 80 | // } |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 81 | // } |
| 82 | |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 83 | m_wantCongestionMarking = context.generalConfig.wantCongestionMarking; |
| 84 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 85 | bool wantListen = true; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 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 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 100 | if (key == "listen") { |
| 101 | wantListen = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 102 | } |
| 103 | else if (key == "port") { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 104 | port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"); |
| 105 | } |
| 106 | else if (key == "enable_v4") { |
| 107 | enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 108 | } |
| 109 | else if (key == "enable_v6") { |
| 110 | enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 111 | } |
| 112 | else if (key == "idle_timeout") { |
| 113 | idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp"); |
| 114 | } |
| 115 | else if (key == "keep_alive_interval") { |
| 116 | // ignored |
| 117 | } |
| 118 | else if (key == "mcast") { |
| 119 | mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 120 | } |
| 121 | else if (key == "mcast_group") { |
| 122 | const std::string& valueStr = value.get_value<std::string>(); |
| 123 | boost::system::error_code ec; |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 124 | mcastConfig.group.address(ip::address_v4::from_string(valueStr, ec)); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 125 | if (ec) { |
| 126 | BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" + |
| 127 | valueStr + "' cannot be parsed as an IPv4 address")); |
| 128 | } |
| 129 | else if (!mcastConfig.group.address().is_multicast()) { |
| 130 | BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group: '" + |
| 131 | valueStr + "' is not a multicast address")); |
| 132 | } |
| 133 | } |
| 134 | else if (key == "mcast_port") { |
| 135 | mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp")); |
| 136 | } |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 137 | else if (key == "mcast_group_v6") { |
| 138 | const std::string& valueStr = value.get_value<std::string>(); |
| 139 | boost::system::error_code ec; |
| 140 | mcastConfig.groupV6.address(ndn::ip::addressV6FromString(valueStr, ec)); |
| 141 | if (ec) { |
| 142 | BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group_v6: '" + |
| 143 | valueStr + "' cannot be parsed as an IPv6 address")); |
| 144 | } |
| 145 | else if (!mcastConfig.groupV6.address().is_multicast()) { |
| 146 | BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.udp.mcast_group_v6: '" + |
| 147 | valueStr + "' is not a multicast address")); |
| 148 | } |
| 149 | } |
| 150 | else if (key == "mcast_port_v6") { |
| 151 | mcastConfig.groupV6.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp")); |
| 152 | } |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 153 | else if (key == "mcast_ad_hoc") { |
| 154 | bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 155 | mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS; |
| 156 | } |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 157 | else if (key == "whitelist") { |
| 158 | mcastConfig.netifPredicate.parseWhitelist(value); |
| 159 | } |
| 160 | else if (key == "blacklist") { |
| 161 | mcastConfig.netifPredicate.parseBlacklist(value); |
| 162 | } |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 163 | else { |
| 164 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.udp." + key)); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) { |
| 169 | BOOST_THROW_EXCEPTION(ConfigFile::Error( |
| 170 | "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. " |
| 171 | "Remove face_system.udp section to disable UDP channels or enable at least one of them.")); |
| 172 | } |
| 173 | } |
| 174 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 175 | if (context.isDryRun) { |
| 176 | return; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 177 | } |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 178 | |
| 179 | if (enableV4) { |
| 180 | udp::Endpoint endpoint(ip::udp::v4(), port); |
| 181 | shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout)); |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 182 | if (wantListen && !v4Channel->isListening()) { |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 183 | v4Channel->listen(this->addFace, nullptr); |
| 184 | } |
| 185 | providedSchemes.insert("udp"); |
| 186 | providedSchemes.insert("udp4"); |
| 187 | } |
| 188 | else if (providedSchemes.count("udp4") > 0) { |
| 189 | NFD_LOG_WARN("Cannot close udp4 channel after its creation"); |
| 190 | } |
| 191 | |
| 192 | if (enableV6) { |
| 193 | udp::Endpoint endpoint(ip::udp::v6(), port); |
| 194 | shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout)); |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 195 | if (wantListen && !v6Channel->isListening()) { |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 196 | v6Channel->listen(this->addFace, nullptr); |
| 197 | } |
| 198 | providedSchemes.insert("udp"); |
| 199 | providedSchemes.insert("udp6"); |
| 200 | } |
| 201 | else if (providedSchemes.count("udp6") > 0) { |
| 202 | NFD_LOG_WARN("Cannot close udp6 channel after its creation"); |
| 203 | } |
| 204 | |
| 205 | if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) { |
| 206 | if (mcastConfig.isEnabled) { |
| 207 | NFD_LOG_INFO("enabling multicast on " << mcastConfig.group); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 208 | NFD_LOG_INFO("enabling multicast on " << mcastConfig.groupV6); |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 209 | } |
| 210 | else { |
| 211 | NFD_LOG_INFO("disabling multicast"); |
| 212 | } |
| 213 | } |
| 214 | else if (mcastConfig.isEnabled) { |
| 215 | if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) { |
| 216 | NFD_LOG_WARN("Cannot change ad hoc setting on existing faces"); |
| 217 | } |
| 218 | if (m_mcastConfig.group != mcastConfig.group) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 219 | NFD_LOG_INFO("changing IPv4 multicast group from " << m_mcastConfig.group << |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 220 | " to " << mcastConfig.group); |
| 221 | } |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 222 | if (m_mcastConfig.groupV6 != mcastConfig.groupV6) { |
| 223 | NFD_LOG_INFO("changing IPv6 multicast group from " << m_mcastConfig.groupV6 << |
| 224 | " to " << mcastConfig.groupV6); |
| 225 | } |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 226 | if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) { |
| 227 | NFD_LOG_INFO("changing whitelist/blacklist"); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // Even if there's no configuration change, we still need to re-apply configuration because |
| 232 | // netifs may have changed. |
| 233 | m_mcastConfig = mcastConfig; |
| 234 | this->applyMcastConfig(context); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 238 | UdpFactory::createFace(const CreateFaceRequest& req, |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 239 | const FaceCreatedCallback& onCreated, |
| 240 | const FaceCreationFailedCallback& onFailure) |
| 241 | { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 242 | BOOST_ASSERT(req.remoteUri.isCanonical()); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 243 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 244 | if (req.localUri) { |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 245 | NFD_LOG_TRACE("Cannot create unicast UDP face with LocalUri"); |
| 246 | onFailure(406, "Unicast UDP faces cannot be created with a LocalUri"); |
| 247 | return; |
| 248 | } |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 249 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 250 | if (req.params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 251 | NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND"); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 252 | onFailure(406, "Outgoing UDP faces do not support on-demand persistency"); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 256 | udp::Endpoint endpoint(ndn::ip::addressFromString(req.remoteUri.getHost()), |
| 257 | boost::lexical_cast<uint16_t>(req.remoteUri.getPort())); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 258 | |
| 259 | if (endpoint.address().is_multicast()) { |
| 260 | NFD_LOG_TRACE("createFace does not support multicast faces"); |
| 261 | onFailure(406, "Cannot create multicast UDP faces"); |
| 262 | return; |
| 263 | } |
| 264 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 265 | if (req.params.wantLocalFields) { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 266 | // UDP faces are never local |
| 267 | NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled"); |
| 268 | onFailure(406, "Local fields can only be enabled on faces with local scope"); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | // very simple logic for now |
| 273 | for (const auto& i : m_channels) { |
| 274 | if ((i.first.address().is_v4() && endpoint.address().is_v4()) || |
| 275 | (i.first.address().is_v6() && endpoint.address().is_v6())) { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 276 | i.second->connect(endpoint, req.params, onCreated, onFailure); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 277 | return; |
| 278 | } |
| 279 | } |
| 280 | |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 281 | NFD_LOG_TRACE("No channels available to connect to " << endpoint); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 282 | onFailure(504, "No channels available to connect"); |
| 283 | } |
| 284 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 285 | shared_ptr<UdpChannel> |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 286 | UdpFactory::createChannel(const udp::Endpoint& localEndpoint, |
| 287 | time::nanoseconds idleTimeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 288 | { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 289 | auto it = m_channels.find(localEndpoint); |
| 290 | if (it != m_channels.end()) |
| 291 | return it->second; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 292 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 293 | // check if the endpoint is already used by a multicast face |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 294 | if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 295 | BOOST_THROW_EXCEPTION(Error("Cannot create UDP channel on " + |
| 296 | boost::lexical_cast<std::string>(localEndpoint) + |
| 297 | ", endpoint already allocated for a UDP multicast face")); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 298 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 299 | |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 300 | auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout, m_wantCongestionMarking); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 301 | m_channels[localEndpoint] = channel; |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 302 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 303 | return channel; |
| 304 | } |
| 305 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 306 | std::vector<shared_ptr<const Channel>> |
| 307 | UdpFactory::getChannels() const |
| 308 | { |
| 309 | return getChannelsFromMap(m_channels); |
| 310 | } |
| 311 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 312 | shared_ptr<Face> |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 313 | UdpFactory::createMulticastFace(const shared_ptr<const net::NetworkInterface>& netif, |
| 314 | const ip::address& localAddress, |
| 315 | const udp::Endpoint& multicastEndpoint) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 316 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 317 | BOOST_ASSERT(multicastEndpoint.address().is_multicast()); |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 318 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 319 | udp::Endpoint localEp(localAddress, multicastEndpoint.port()); |
| 320 | BOOST_ASSERT(localEp.protocol() == multicastEndpoint.protocol()); |
| 321 | |
| 322 | auto mcastEp = multicastEndpoint; |
| 323 | if (mcastEp.address().is_v6()) { |
| 324 | // in IPv6, a scope id on the multicast address is always required |
| 325 | auto mcastAddress = mcastEp.address().to_v6(); |
| 326 | mcastAddress.scope_id(netif->getIndex()); |
| 327 | mcastEp.address(mcastAddress); |
| 328 | } |
| 329 | |
| 330 | // check if the local endpoint is already used by another multicast face |
| 331 | auto it = m_mcastFaces.find(localEp); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 332 | if (it != m_mcastFaces.end()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 333 | if (it->second->getRemoteUri() == FaceUri(mcastEp)) |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 334 | return it->second; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 335 | else |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 336 | BOOST_THROW_EXCEPTION(Error("Cannot create UDP multicast face on " + |
| 337 | boost::lexical_cast<std::string>(localEp) + |
| 338 | ", endpoint already allocated for a different UDP multicast face")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 339 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 340 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 341 | // check if the local endpoint is already used by a unicast channel |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 342 | if (m_channels.find(localEp) != m_channels.end()) { |
| 343 | BOOST_THROW_EXCEPTION(Error("Cannot create UDP multicast face on " + |
| 344 | boost::lexical_cast<std::string>(localEp) + |
| 345 | ", endpoint already allocated for a UDP channel")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 346 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 347 | |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 348 | ip::udp::socket rxSock(getGlobalIoService()); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 349 | MulticastUdpTransport::openRxSocket(rxSock, mcastEp, localAddress, netif); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 350 | ip::udp::socket txSock(getGlobalIoService()); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 351 | MulticastUdpTransport::openTxSocket(txSock, udp::Endpoint(localAddress, 0), netif); |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 352 | |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 353 | GenericLinkService::Options options; |
| 354 | options.allowCongestionMarking = m_wantCongestionMarking; |
| 355 | auto linkService = make_unique<GenericLinkService>(options); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 356 | auto transport = make_unique<MulticastUdpTransport>(mcastEp, std::move(rxSock), std::move(txSock), |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 357 | m_mcastConfig.linkType); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 358 | auto face = make_shared<Face>(std::move(linkService), std::move(transport)); |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 359 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 360 | m_mcastFaces[localEp] = face; |
| 361 | connectFaceClosedSignal(*face, [this, localEp] { m_mcastFaces.erase(localEp); }); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 362 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 363 | return face; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 366 | static ndn::optional<ip::address> |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 367 | pickAddress(const net::NetworkInterface& netif, net::AddressFamily af) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 368 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 369 | for (const auto& na : netif.getNetworkAddresses()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 370 | if (na.getFamily() == af && |
| 371 | (na.getScope() == net::AddressScope::LINK || na.getScope() == net::AddressScope::GLOBAL)) { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 372 | return na.getIp(); |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | return ndn::nullopt; |
| 376 | } |
| 377 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 378 | std::vector<shared_ptr<Face>> |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 379 | UdpFactory::applyMcastConfigToNetif(const shared_ptr<const net::NetworkInterface>& netif) |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 380 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 381 | BOOST_ASSERT(netif != nullptr); |
| 382 | |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 383 | if (!m_mcastConfig.isEnabled) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 384 | return {}; |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | if (!netif->isUp()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 388 | NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is down"); |
| 389 | return {}; |
| 390 | } |
| 391 | |
| 392 | if (netif->isLoopback()) { |
| 393 | NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is loopback"); |
| 394 | return {}; |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | if (!netif->canMulticast()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 398 | NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif cannot multicast"); |
| 399 | return {}; |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | if (!m_mcastConfig.netifPredicate(*netif)) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 403 | NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": rejected by whitelist/blacklist"); |
| 404 | return {}; |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 407 | std::vector<ip::address> addrs; |
| 408 | for (auto af : {net::AddressFamily::V4, net::AddressFamily::V6}) { |
| 409 | auto addr = pickAddress(*netif, af); |
| 410 | if (addr) |
| 411 | addrs.push_back(*addr); |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 412 | } |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 413 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 414 | if (addrs.empty()) { |
| 415 | NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": no viable IP address"); |
| 416 | // keep an eye on new addresses |
| 417 | m_netifConns[netif->getIndex()].addrAddConn = |
| 418 | netif->onAddressAdded.connect(bind(&UdpFactory::applyMcastConfigToNetif, this, netif)); |
| 419 | return {}; |
| 420 | } |
| 421 | |
| 422 | NFD_LOG_DEBUG("Creating multicast faces on " << netif->getName()); |
| 423 | |
| 424 | std::vector<shared_ptr<Face>> faces; |
| 425 | for (const auto& addr : addrs) { |
| 426 | auto face = this->createMulticastFace(netif, addr, |
| 427 | addr.is_v4() ? m_mcastConfig.group : m_mcastConfig.groupV6); |
| 428 | if (face->getId() == INVALID_FACEID) { |
| 429 | // new face: register with forwarding |
| 430 | this->addFace(face); |
| 431 | } |
| 432 | faces.push_back(std::move(face)); |
| 433 | } |
| 434 | |
| 435 | return faces; |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 438 | void |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 439 | UdpFactory::applyMcastConfig(const FaceSystem::ConfigContext& context) |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 440 | { |
| 441 | // collect old faces |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 442 | std::set<shared_ptr<Face>> facesToClose; |
| 443 | boost::copy(m_mcastFaces | boost::adaptors::map_values, |
| 444 | std::inserter(facesToClose, facesToClose.end())); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 445 | |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 446 | // create faces if requested by config |
| 447 | for (const auto& netif : netmon->listNetworkInterfaces()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 448 | auto facesToKeep = this->applyMcastConfigToNetif(netif); |
| 449 | for (const auto& face : facesToKeep) { |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 450 | // don't destroy face |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 451 | facesToClose.erase(face); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | // destroy old faces that are not needed in new configuration |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 456 | for (const auto& face : facesToClose) { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 457 | face->close(); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | } // namespace face |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 462 | } // namespace nfd |