Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | 1d7e7af | 2015-10-10 23:54:08 +0200 | [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 | |
| 43 | void |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 44 | TcpFactory::processConfig(OptionalConfigSection configSection, |
| 45 | FaceSystem::ConfigContext& context) |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 46 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 47 | // tcp |
| 48 | // { |
| 49 | // listen yes |
| 50 | // port 6363 |
| 51 | // enable_v4 yes |
| 52 | // enable_v6 yes |
| 53 | // } |
| 54 | |
| 55 | if (!configSection) { |
| 56 | if (!context.isDryRun && !m_channels.empty()) { |
| 57 | NFD_LOG_WARN("Cannot disable tcp4 and tcp6 channels after initialization"); |
| 58 | } |
| 59 | return; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 60 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 61 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 62 | bool wantListen = true; |
| 63 | uint16_t port = 6363; |
| 64 | bool enableV4 = true; |
| 65 | bool enableV6 = true; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 66 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 67 | for (const auto& pair : *configSection) { |
| 68 | const std::string& key = pair.first; |
| 69 | |
| 70 | if (key == "listen") { |
| 71 | wantListen = ConfigFile::parseYesNo(pair, "face_system.tcp"); |
| 72 | } |
| 73 | else if (key == "port") { |
| 74 | port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.tcp"); |
| 75 | } |
| 76 | else if (key == "enable_v4") { |
| 77 | enableV4 = ConfigFile::parseYesNo(pair, "face_system.tcp"); |
| 78 | } |
| 79 | else if (key == "enable_v6") { |
| 80 | enableV6 = ConfigFile::parseYesNo(pair, "face_system.tcp"); |
| 81 | } |
| 82 | else { |
| 83 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.tcp." + key)); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 84 | } |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 85 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 86 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 87 | if (!enableV4 && !enableV6) { |
| 88 | BOOST_THROW_EXCEPTION(ConfigFile::Error( |
| 89 | "IPv4 and IPv6 TCP channels have been disabled. Remove face_system.tcp section to disable " |
| 90 | "TCP channels or enable at least one channel type.")); |
| 91 | } |
| 92 | |
| 93 | if (!context.isDryRun) { |
| 94 | providedSchemes.insert("tcp"); |
| 95 | |
| 96 | if (enableV4) { |
| 97 | tcp::Endpoint endpoint(ip::tcp::v4(), port); |
| 98 | shared_ptr<TcpChannel> v4Channel = this->createChannel(endpoint); |
| 99 | if (wantListen && !v4Channel->isListening()) { |
| 100 | v4Channel->listen(context.addFace, nullptr); |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 101 | } |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 102 | providedSchemes.insert("tcp4"); |
| 103 | } |
| 104 | else if (providedSchemes.count("tcp4") > 0) { |
| 105 | NFD_LOG_WARN("Cannot close tcp4 channel after its creation"); |
| 106 | } |
| 107 | |
| 108 | if (enableV6) { |
| 109 | tcp::Endpoint endpoint(ip::tcp::v6(), port); |
| 110 | shared_ptr<TcpChannel> v6Channel = this->createChannel(endpoint); |
| 111 | if (wantListen && !v6Channel->isListening()) { |
| 112 | v6Channel->listen(context.addFace, nullptr); |
| 113 | } |
| 114 | providedSchemes.insert("tcp6"); |
| 115 | } |
| 116 | else if (providedSchemes.count("tcp6") > 0) { |
| 117 | NFD_LOG_WARN("Cannot close tcp6 channel after its creation"); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 118 | } |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 119 | } |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 122 | void |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 123 | TcpFactory::createFace(const FaceUri& remoteUri, |
| 124 | const ndn::optional<FaceUri>& localUri, |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 125 | ndn::nfd::FacePersistency persistency, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 126 | bool wantLocalFieldsEnabled, |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 127 | const FaceCreatedCallback& onCreated, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 128 | const FaceCreationFailedCallback& onFailure) |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 129 | { |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 130 | BOOST_ASSERT(remoteUri.isCanonical()); |
| 131 | |
| 132 | if (localUri) { |
| 133 | NFD_LOG_TRACE("Cannot create unicast TCP face with LocalUri"); |
| 134 | onFailure(406, "Unicast TCP faces cannot be created with a LocalUri"); |
| 135 | return; |
| 136 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 137 | |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 138 | if (persistency != ndn::nfd::FACE_PERSISTENCY_PERSISTENT) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 139 | NFD_LOG_TRACE("createFace only supports FACE_PERSISTENCY_PERSISTENT"); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 140 | onFailure(406, "Outgoing TCP faces only support persistent persistency"); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 141 | return; |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 142 | } |
| 143 | |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 144 | tcp::Endpoint endpoint(ip::address::from_string(remoteUri.getHost()), |
| 145 | boost::lexical_cast<uint16_t>(remoteUri.getPort())); |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 146 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 147 | if (endpoint.address().is_multicast()) { |
| 148 | NFD_LOG_TRACE("createFace cannot create multicast faces"); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 149 | onFailure(406, "Cannot create multicast TCP faces"); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 153 | if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 154 | NFD_LOG_TRACE("Requested endpoint is prohibited " |
| 155 | "(reserved by NFD or disallowed by face management protocol)"); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 156 | onFailure(406, "Requested endpoint is prohibited"); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if (wantLocalFieldsEnabled && !endpoint.address().is_loopback()) { |
| 161 | NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled"); |
| 162 | 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] | 163 | return; |
| 164 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 165 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 166 | // very simple logic for now |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 167 | for (const auto& i : m_channels) { |
| 168 | if ((i.first.address().is_v4() && endpoint.address().is_v4()) || |
| 169 | (i.first.address().is_v6() && endpoint.address().is_v6())) { |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 170 | i.second->connect(endpoint, wantLocalFieldsEnabled, onCreated, onFailure); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 171 | return; |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 172 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 175 | NFD_LOG_TRACE("No channels available to connect to " + boost::lexical_cast<std::string>(endpoint)); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 176 | onFailure(504, "No channels available to connect"); |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 179 | void |
| 180 | TcpFactory::prohibitEndpoint(const tcp::Endpoint& endpoint) |
| 181 | { |
| 182 | if (endpoint.address().is_v4() && |
| 183 | endpoint.address() == ip::address_v4::any()) { |
| 184 | prohibitAllIpv4Endpoints(endpoint.port()); |
| 185 | } |
| 186 | else if (endpoint.address().is_v6() && |
| 187 | endpoint.address() == ip::address_v6::any()) { |
| 188 | prohibitAllIpv6Endpoints(endpoint.port()); |
| 189 | } |
| 190 | |
| 191 | NFD_LOG_TRACE("prohibiting TCP " << endpoint); |
| 192 | m_prohibitedEndpoints.insert(endpoint); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | TcpFactory::prohibitAllIpv4Endpoints(uint16_t port) |
| 197 | { |
| 198 | ///\todo prohibited endpoints need to react to dynamic NIC changes |
| 199 | for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) { |
| 200 | for (const auto& addr : nic.ipv4Addresses) { |
| 201 | if (addr != ip::address_v4::any()) { |
| 202 | prohibitEndpoint(tcp::Endpoint(addr, port)); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | TcpFactory::prohibitAllIpv6Endpoints(uint16_t port) |
| 210 | { |
| 211 | ///\todo prohibited endpoints need to react to dynamic NIC changes |
| 212 | for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) { |
| 213 | for (const auto& addr : nic.ipv6Addresses) { |
| 214 | if (addr != ip::address_v6::any()) { |
| 215 | prohibitEndpoint(tcp::Endpoint(addr, port)); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | shared_ptr<TcpChannel> |
| 222 | TcpFactory::createChannel(const tcp::Endpoint& endpoint) |
| 223 | { |
| 224 | auto channel = findChannel(endpoint); |
| 225 | if (channel) |
| 226 | return channel; |
| 227 | |
| 228 | channel = make_shared<TcpChannel>(endpoint); |
| 229 | m_channels[endpoint] = channel; |
| 230 | prohibitEndpoint(endpoint); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 231 | return channel; |
| 232 | } |
| 233 | |
| 234 | shared_ptr<TcpChannel> |
| 235 | TcpFactory::createChannel(const std::string& localIp, const std::string& localPort) |
| 236 | { |
| 237 | tcp::Endpoint endpoint(ip::address::from_string(localIp), |
| 238 | boost::lexical_cast<uint16_t>(localPort)); |
| 239 | return createChannel(endpoint); |
| 240 | } |
| 241 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 242 | std::vector<shared_ptr<const Channel>> |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 243 | TcpFactory::getChannels() const |
| 244 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 245 | return getChannelsFromMap(m_channels); |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 246 | } |
| 247 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 248 | shared_ptr<TcpChannel> |
| 249 | TcpFactory::findChannel(const tcp::Endpoint& localEndpoint) const |
| 250 | { |
| 251 | auto i = m_channels.find(localEndpoint); |
| 252 | if (i != m_channels.end()) |
| 253 | return i->second; |
| 254 | else |
| 255 | return nullptr; |
| 256 | } |
| 257 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 258 | } // namespace face |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 259 | } // namespace nfd |