blob: 8ff04cc8148dec98c05997586c589e3cecd9c78e [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2d491752017-07-14 21:32:05 +00002/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Chengyu Fan4381fb62015-01-14 11:37:04 -07004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080025
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080026#include "tcp-factory.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080027
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040028namespace nfd::face {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080029
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020030namespace ip = boost::asio::ip;
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080031
Davide Pesaventoa3148082018-04-12 18:21:54 -040032NFD_LOG_INIT(TcpFactory);
Junxiao Shib47247d2017-01-24 15:09:16 +000033NFD_REGISTER_PROTOCOL_FACTORY(TcpFactory);
34
35const std::string&
Davide Pesaventod27841b2018-11-13 00:22:24 -050036TcpFactory::getId() noexcept
Junxiao Shib47247d2017-01-24 15:09:16 +000037{
38 static std::string id("tcp");
39 return id;
40}
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060041
42void
Davide Pesaventod27841b2018-11-13 00:22:24 -050043TcpFactory::doProcessConfig(OptionalConfigSection configSection,
44 FaceSystem::ConfigContext& context)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060045{
Junxiao Shi38b24c72017-01-05 02:59:31 +000046 // tcp
47 // {
48 // listen yes
49 // port 6363
50 // enable_v4 yes
51 // enable_v6 yes
52 // }
53
Eric Newberry0c841642018-01-17 15:01:00 -070054 m_wantCongestionMarking = context.generalConfig.wantCongestionMarking;
55
Junxiao Shi38b24c72017-01-05 02:59:31 +000056 if (!configSection) {
57 if (!context.isDryRun && !m_channels.empty()) {
Davide Pesaventod27841b2018-11-13 00:22:24 -050058 NFD_LOG_WARN("Cannot disable TCP channels after initialization");
Junxiao Shi38b24c72017-01-05 02:59:31 +000059 }
60 return;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020061 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060062
Junxiao Shi38b24c72017-01-05 02:59:31 +000063 bool wantListen = true;
64 uint16_t port = 6363;
65 bool enableV4 = true;
66 bool enableV6 = true;
Alexander Afanasyevded17422018-04-03 19:00:23 -040067 IpAddressPredicate local;
68 bool isLocalConfigured = false;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060069
Junxiao Shi38b24c72017-01-05 02:59:31 +000070 for (const auto& pair : *configSection) {
71 const std::string& key = pair.first;
72
73 if (key == "listen") {
74 wantListen = ConfigFile::parseYesNo(pair, "face_system.tcp");
75 }
76 else if (key == "port") {
77 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.tcp");
78 }
79 else if (key == "enable_v4") {
80 enableV4 = ConfigFile::parseYesNo(pair, "face_system.tcp");
81 }
82 else if (key == "enable_v6") {
83 enableV6 = ConfigFile::parseYesNo(pair, "face_system.tcp");
84 }
Alexander Afanasyevded17422018-04-03 19:00:23 -040085 else if (key == "local") {
86 isLocalConfigured = true;
87 for (const auto& localPair : pair.second) {
88 const std::string& localKey = localPair.first;
89 if (localKey == "whitelist") {
90 local.parseWhitelist(localPair.second);
91 }
92 else if (localKey == "blacklist") {
93 local.parseBlacklist(localPair.second);
94 }
95 else {
Davide Pesavento19779d82019-02-14 13:40:04 -050096 NDN_THROW(ConfigFile::Error("Unrecognized option face_system.tcp.local." + localKey));
Alexander Afanasyevded17422018-04-03 19:00:23 -040097 }
98 }
99 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000100 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500101 NDN_THROW(ConfigFile::Error("Unrecognized option face_system.tcp." + key));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600102 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100103 }
Alexander Afanasyevded17422018-04-03 19:00:23 -0400104 if (!isLocalConfigured) {
105 local.assign({{"subnet", "127.0.0.0/8"}, {"subnet", "::1/128"}}, {});
106 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600107
Junxiao Shi38b24c72017-01-05 02:59:31 +0000108 if (!enableV4 && !enableV6) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500109 NDN_THROW(ConfigFile::Error(
Junxiao Shi38b24c72017-01-05 02:59:31 +0000110 "IPv4 and IPv6 TCP channels have been disabled. Remove face_system.tcp section to disable "
111 "TCP channels or enable at least one channel type."));
112 }
113
Davide Pesaventod27841b2018-11-13 00:22:24 -0500114 if (context.isDryRun) {
115 return;
Davide Pesaventob499a602014-11-18 22:36:56 +0100116 }
Davide Pesaventod27841b2018-11-13 00:22:24 -0500117
118 providedSchemes.insert("tcp");
119
120 if (enableV4) {
121 tcp::Endpoint endpoint(ip::tcp::v4(), port);
122 auto v4Channel = this->createChannel(endpoint);
123 if (wantListen && !v4Channel->isListening()) {
124 v4Channel->listen(this->addFace, nullptr);
125 }
126 providedSchemes.insert("tcp4");
127 }
128 else if (providedSchemes.count("tcp4") > 0) {
129 NFD_LOG_WARN("Cannot close tcp4 channel after its creation");
130 }
131
132 if (enableV6) {
133 tcp::Endpoint endpoint(ip::tcp::v6(), port);
134 auto v6Channel = this->createChannel(endpoint);
135 if (wantListen && !v6Channel->isListening()) {
136 v6Channel->listen(this->addFace, nullptr);
137 }
138 providedSchemes.insert("tcp6");
139 }
140 else if (providedSchemes.count("tcp6") > 0) {
141 NFD_LOG_WARN("Cannot close tcp6 channel after its creation");
142 }
143
144 m_local = std::move(local);
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800145}
146
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800147void
Davide Pesaventod27841b2018-11-13 00:22:24 -0500148TcpFactory::doCreateFace(const CreateFaceRequest& req,
149 const FaceCreatedCallback& onCreated,
150 const FaceCreationFailedCallback& onFailure)
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800151{
Davide Pesavento15b55052018-01-27 19:09:28 -0500152 if (req.localUri) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000153 NFD_LOG_TRACE("Cannot create unicast TCP face with LocalUri");
154 onFailure(406, "Unicast TCP faces cannot be created with a LocalUri");
155 return;
156 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200157
Davide Pesavento15b55052018-01-27 19:09:28 -0500158 if (req.params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400159 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
160 onFailure(406, "Outgoing TCP faces do not support on-demand persistency");
Eric Newberry42602412016-08-27 09:33:18 -0700161 return;
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800162 }
163
Davide Pesavento9c33b902018-05-20 01:30:29 -0400164 tcp::Endpoint endpoint(ip::address::from_string(req.remoteUri.getHost()),
Davide Pesavento15b55052018-01-27 19:09:28 -0500165 boost::lexical_cast<uint16_t>(req.remoteUri.getPort()));
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800166
Davide Pesavento46afec42017-05-28 14:28:47 -0400167 // a canonical tcp4/tcp6 FaceUri cannot have a multicast address
168 BOOST_ASSERT(!endpoint.address().is_multicast());
Eric Newberry42602412016-08-27 09:33:18 -0700169
Davide Pesavento15b55052018-01-27 19:09:28 -0500170 if (req.params.wantLocalFields && !endpoint.address().is_loopback()) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700171 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
172 onFailure(406, "Local fields can only be enabled on faces with local scope");
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200173 return;
174 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600175
Eric Newberry812d6152018-06-06 15:06:01 -0700176 if (req.params.mtu) {
177 NFD_LOG_TRACE("createFace cannot create a TCP face with an overridden MTU");
178 onFailure(406, "TCP faces do not support MTU overrides");
179 return;
180 }
181
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800182 // very simple logic for now
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200183 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 Pesavento15b55052018-01-27 19:09:28 -0500186 i.second->connect(endpoint, req.params, onCreated, onFailure);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200187 return;
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800188 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200189 }
190
Davide Pesavento46afec42017-05-28 14:28:47 -0400191 NFD_LOG_TRACE("No channels available to connect to " << endpoint);
Eric Newberryf40551a2016-09-05 15:41:16 -0700192 onFailure(504, "No channels available to connect");
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800193}
194
Junxiao Shi38b24c72017-01-05 02:59:31 +0000195shared_ptr<TcpChannel>
196TcpFactory::createChannel(const tcp::Endpoint& endpoint)
197{
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400198 auto it = m_channels.find(endpoint);
199 if (it != m_channels.end())
200 return it->second;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000201
Davide Pesavento412c9822021-07-02 00:21:05 -0400202 auto channel = make_shared<TcpChannel>(endpoint, m_wantCongestionMarking, [this] (auto&&... args) {
203 return determineFaceScopeFromAddresses(std::forward<decltype(args)>(args)...);
204 });
Junxiao Shi38b24c72017-01-05 02:59:31 +0000205 m_channels[endpoint] = channel;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000206 return channel;
207}
208
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200209std::vector<shared_ptr<const Channel>>
Davide Pesaventod27841b2018-11-13 00:22:24 -0500210TcpFactory::doGetChannels() const
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600211{
Junxiao Shi38b24c72017-01-05 02:59:31 +0000212 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600213}
214
Alexander Afanasyevded17422018-04-03 19:00:23 -0400215ndn::nfd::FaceScope
216TcpFactory::determineFaceScopeFromAddresses(const boost::asio::ip::address& localAddress,
217 const boost::asio::ip::address& remoteAddress) const
218{
219 if (m_local(localAddress) && m_local(remoteAddress)) {
220 return ndn::nfd::FACE_SCOPE_LOCAL;
221 }
222 return ndn::nfd::FACE_SCOPE_NON_LOCAL;
223}
224
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400225} // namespace nfd::face