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