Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [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 | /* |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [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 | */ |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 26 | #include "tcp-factory.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 27 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 28 | #include <boost/lexical_cast.hpp> |
| 29 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 30 | namespace nfd::face { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 31 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 32 | namespace ip = boost::asio::ip; |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 33 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 34 | NFD_LOG_INIT(TcpFactory); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 35 | NFD_REGISTER_PROTOCOL_FACTORY(TcpFactory); |
| 36 | |
| 37 | const std::string& |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 38 | TcpFactory::getId() noexcept |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 39 | { |
| 40 | static std::string id("tcp"); |
| 41 | return id; |
| 42 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 43 | |
| 44 | void |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 45 | TcpFactory::doProcessConfig(OptionalConfigSection configSection, |
| 46 | FaceSystem::ConfigContext& context) |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 47 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 48 | // tcp |
| 49 | // { |
| 50 | // listen yes |
| 51 | // port 6363 |
| 52 | // enable_v4 yes |
| 53 | // enable_v6 yes |
| 54 | // } |
| 55 | |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 56 | m_wantCongestionMarking = context.generalConfig.wantCongestionMarking; |
| 57 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 58 | if (!configSection) { |
| 59 | if (!context.isDryRun && !m_channels.empty()) { |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 60 | NFD_LOG_WARN("Cannot disable TCP channels after initialization"); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 61 | } |
| 62 | return; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 63 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 64 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 65 | bool wantListen = true; |
| 66 | uint16_t port = 6363; |
| 67 | bool enableV4 = true; |
| 68 | bool enableV6 = true; |
Alexander Afanasyev | ded1742 | 2018-04-03 19:00:23 -0400 | [diff] [blame] | 69 | IpAddressPredicate local; |
| 70 | bool isLocalConfigured = false; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 71 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 72 | for (const auto& pair : *configSection) { |
| 73 | const std::string& key = pair.first; |
| 74 | |
| 75 | if (key == "listen") { |
| 76 | wantListen = ConfigFile::parseYesNo(pair, "face_system.tcp"); |
| 77 | } |
| 78 | else if (key == "port") { |
| 79 | port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.tcp"); |
| 80 | } |
| 81 | else if (key == "enable_v4") { |
| 82 | enableV4 = ConfigFile::parseYesNo(pair, "face_system.tcp"); |
| 83 | } |
| 84 | else if (key == "enable_v6") { |
| 85 | enableV6 = ConfigFile::parseYesNo(pair, "face_system.tcp"); |
| 86 | } |
Alexander Afanasyev | ded1742 | 2018-04-03 19:00:23 -0400 | [diff] [blame] | 87 | else if (key == "local") { |
| 88 | isLocalConfigured = true; |
| 89 | for (const auto& localPair : pair.second) { |
| 90 | const std::string& localKey = localPair.first; |
| 91 | if (localKey == "whitelist") { |
| 92 | local.parseWhitelist(localPair.second); |
| 93 | } |
| 94 | else if (localKey == "blacklist") { |
| 95 | local.parseBlacklist(localPair.second); |
| 96 | } |
| 97 | else { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 98 | NDN_THROW(ConfigFile::Error("Unrecognized option face_system.tcp.local." + localKey)); |
Alexander Afanasyev | ded1742 | 2018-04-03 19:00:23 -0400 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 102 | else { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 103 | NDN_THROW(ConfigFile::Error("Unrecognized option face_system.tcp." + key)); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 104 | } |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 105 | } |
Alexander Afanasyev | ded1742 | 2018-04-03 19:00:23 -0400 | [diff] [blame] | 106 | if (!isLocalConfigured) { |
| 107 | local.assign({{"subnet", "127.0.0.0/8"}, {"subnet", "::1/128"}}, {}); |
| 108 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 109 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 110 | if (!enableV4 && !enableV6) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 111 | NDN_THROW(ConfigFile::Error( |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 112 | "IPv4 and IPv6 TCP channels have been disabled. Remove face_system.tcp section to disable " |
| 113 | "TCP channels or enable at least one channel type.")); |
| 114 | } |
| 115 | |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 116 | if (context.isDryRun) { |
| 117 | return; |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 118 | } |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 119 | |
| 120 | providedSchemes.insert("tcp"); |
| 121 | |
| 122 | if (enableV4) { |
| 123 | tcp::Endpoint endpoint(ip::tcp::v4(), port); |
| 124 | auto v4Channel = this->createChannel(endpoint); |
| 125 | if (wantListen && !v4Channel->isListening()) { |
| 126 | v4Channel->listen(this->addFace, nullptr); |
| 127 | } |
| 128 | providedSchemes.insert("tcp4"); |
| 129 | } |
| 130 | else if (providedSchemes.count("tcp4") > 0) { |
| 131 | NFD_LOG_WARN("Cannot close tcp4 channel after its creation"); |
| 132 | } |
| 133 | |
| 134 | if (enableV6) { |
| 135 | tcp::Endpoint endpoint(ip::tcp::v6(), port); |
| 136 | auto v6Channel = this->createChannel(endpoint); |
| 137 | if (wantListen && !v6Channel->isListening()) { |
| 138 | v6Channel->listen(this->addFace, nullptr); |
| 139 | } |
| 140 | providedSchemes.insert("tcp6"); |
| 141 | } |
| 142 | else if (providedSchemes.count("tcp6") > 0) { |
| 143 | NFD_LOG_WARN("Cannot close tcp6 channel after its creation"); |
| 144 | } |
| 145 | |
| 146 | m_local = std::move(local); |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 149 | void |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 150 | TcpFactory::doCreateFace(const CreateFaceRequest& req, |
| 151 | const FaceCreatedCallback& onCreated, |
| 152 | const FaceCreationFailedCallback& onFailure) |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 153 | { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 154 | if (req.localUri) { |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 155 | NFD_LOG_TRACE("createFace: unsupported LocalUri"); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 156 | onFailure(406, "Unicast TCP faces cannot be created with a LocalUri"); |
| 157 | return; |
| 158 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 159 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 160 | if (req.params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) { |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 161 | NFD_LOG_TRACE("createFace: unsupported FacePersistency"); |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 162 | onFailure(406, "Outgoing TCP faces do not support on-demand persistency"); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 163 | return; |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 164 | } |
| 165 | |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -0400 | [diff] [blame] | 166 | tcp::Endpoint endpoint(ip::make_address(req.remoteUri.getHost()), |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 167 | boost::lexical_cast<uint16_t>(req.remoteUri.getPort())); |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 168 | |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 169 | // a canonical tcp4/tcp6 FaceUri cannot have a multicast address |
| 170 | BOOST_ASSERT(!endpoint.address().is_multicast()); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 171 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 172 | if (req.params.wantLocalFields && !endpoint.address().is_loopback()) { |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 173 | NFD_LOG_TRACE("createFace: cannot create non-local face with local fields enabled"); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 174 | onFailure(406, "Local fields can only be enabled on faces with local scope"); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 175 | return; |
| 176 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 177 | |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 178 | if (req.params.mtu) { |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 179 | NFD_LOG_TRACE("createFace: cannot create TCP face with overridden MTU"); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 180 | onFailure(406, "TCP faces do not support MTU overrides"); |
| 181 | return; |
| 182 | } |
| 183 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 184 | // very simple logic for now |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 185 | for (const auto& i : m_channels) { |
| 186 | if ((i.first.address().is_v4() && endpoint.address().is_v4()) || |
| 187 | (i.first.address().is_v6() && endpoint.address().is_v6())) { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 188 | i.second->connect(endpoint, req.params, onCreated, onFailure); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 189 | return; |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 190 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 193 | NFD_LOG_TRACE("No channels available to connect to " << endpoint); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 194 | onFailure(504, "No channels available to connect"); |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 197 | shared_ptr<TcpChannel> |
| 198 | TcpFactory::createChannel(const tcp::Endpoint& endpoint) |
| 199 | { |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 200 | auto it = m_channels.find(endpoint); |
| 201 | if (it != m_channels.end()) |
| 202 | return it->second; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 203 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 204 | auto channel = make_shared<TcpChannel>(endpoint, m_wantCongestionMarking, [this] (auto&&... args) { |
| 205 | return determineFaceScopeFromAddresses(std::forward<decltype(args)>(args)...); |
| 206 | }); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 207 | m_channels[endpoint] = channel; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 208 | return channel; |
| 209 | } |
| 210 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 211 | std::vector<shared_ptr<const Channel>> |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 212 | TcpFactory::doGetChannels() const |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 213 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 214 | return getChannelsFromMap(m_channels); |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 215 | } |
| 216 | |
Alexander Afanasyev | ded1742 | 2018-04-03 19:00:23 -0400 | [diff] [blame] | 217 | ndn::nfd::FaceScope |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 218 | TcpFactory::determineFaceScopeFromAddresses(const ip::address& localAddress, |
| 219 | const ip::address& remoteAddress) const |
Alexander Afanasyev | ded1742 | 2018-04-03 19:00:23 -0400 | [diff] [blame] | 220 | { |
| 221 | if (m_local(localAddress) && m_local(remoteAddress)) { |
| 222 | return ndn::nfd::FACE_SCOPE_LOCAL; |
| 223 | } |
| 224 | return ndn::nfd::FACE_SCOPE_NON_LOCAL; |
| 225 | } |
| 226 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 227 | } // namespace nfd::face |