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