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