blob: 28ac25150eecd277f100fd79875009f0dcb037ec [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/*
Junxiao Shi38b24c72017-01-05 02:59:31 +00003 * Copyright (c) 2014-2017, 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
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080028namespace nfd {
Junxiao Shi38b24c72017-01-05 02:59:31 +000029namespace face {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080030
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020031namespace ip = boost::asio::ip;
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080032
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020033NFD_LOG_INIT("TcpFactory");
Junxiao Shib47247d2017-01-24 15:09:16 +000034NFD_REGISTER_PROTOCOL_FACTORY(TcpFactory);
35
36const std::string&
37TcpFactory::getId()
38{
39 static std::string id("tcp");
40 return id;
41}
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060042
Junxiao Shi0ba6d642017-07-17 00:53:22 +000043TcpFactory::TcpFactory(const CtorParams& params)
44 : ProtocolFactory(params)
45{
46}
47
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060048void
Junxiao Shi38b24c72017-01-05 02:59:31 +000049TcpFactory::processConfig(OptionalConfigSection configSection,
50 FaceSystem::ConfigContext& context)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060051{
Junxiao Shi38b24c72017-01-05 02:59:31 +000052 // tcp
53 // {
54 // listen yes
55 // port 6363
56 // enable_v4 yes
57 // enable_v6 yes
58 // }
59
60 if (!configSection) {
61 if (!context.isDryRun && !m_channels.empty()) {
62 NFD_LOG_WARN("Cannot disable tcp4 and tcp6 channels after initialization");
63 }
64 return;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020065 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060066
Junxiao Shi38b24c72017-01-05 02:59:31 +000067 bool wantListen = true;
68 uint16_t port = 6363;
69 bool enableV4 = true;
70 bool enableV6 = true;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060071
Junxiao Shi38b24c72017-01-05 02:59:31 +000072 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 }
87 else {
88 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.tcp." + key));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060089 }
Davide Pesaventob499a602014-11-18 22:36:56 +010090 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060091
Junxiao Shi38b24c72017-01-05 02:59:31 +000092 if (!enableV4 && !enableV6) {
93 BOOST_THROW_EXCEPTION(ConfigFile::Error(
94 "IPv4 and IPv6 TCP channels have been disabled. Remove face_system.tcp section to disable "
95 "TCP channels or enable at least one channel type."));
96 }
97
98 if (!context.isDryRun) {
99 providedSchemes.insert("tcp");
100
101 if (enableV4) {
102 tcp::Endpoint endpoint(ip::tcp::v4(), port);
103 shared_ptr<TcpChannel> v4Channel = this->createChannel(endpoint);
104 if (wantListen && !v4Channel->isListening()) {
Junxiao Shi2d491752017-07-14 21:32:05 +0000105 v4Channel->listen(this->addFace, nullptr);
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800106 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000107 providedSchemes.insert("tcp4");
108 }
109 else if (providedSchemes.count("tcp4") > 0) {
110 NFD_LOG_WARN("Cannot close tcp4 channel after its creation");
111 }
112
113 if (enableV6) {
114 tcp::Endpoint endpoint(ip::tcp::v6(), port);
115 shared_ptr<TcpChannel> v6Channel = this->createChannel(endpoint);
116 if (wantListen && !v6Channel->isListening()) {
Junxiao Shi2d491752017-07-14 21:32:05 +0000117 v6Channel->listen(this->addFace, nullptr);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000118 }
119 providedSchemes.insert("tcp6");
120 }
121 else if (providedSchemes.count("tcp6") > 0) {
122 NFD_LOG_WARN("Cannot close tcp6 channel after its creation");
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600123 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100124 }
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800125}
126
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800127void
Eric Newberry78e32b02017-04-01 14:34:44 +0000128TcpFactory::createFace(const FaceUri& remoteUri,
129 const ndn::optional<FaceUri>& localUri,
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800130 ndn::nfd::FacePersistency persistency,
Eric Newberryf40551a2016-09-05 15:41:16 -0700131 bool wantLocalFieldsEnabled,
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800132 const FaceCreatedCallback& onCreated,
Eric Newberryf40551a2016-09-05 15:41:16 -0700133 const FaceCreationFailedCallback& onFailure)
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800134{
Eric Newberry78e32b02017-04-01 14:34:44 +0000135 BOOST_ASSERT(remoteUri.isCanonical());
136
137 if (localUri) {
138 NFD_LOG_TRACE("Cannot create unicast TCP face with LocalUri");
139 onFailure(406, "Unicast TCP faces cannot be created with a LocalUri");
140 return;
141 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200142
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400143 if (persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
144 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
145 onFailure(406, "Outgoing TCP faces do not support on-demand persistency");
Eric Newberry42602412016-08-27 09:33:18 -0700146 return;
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800147 }
148
Eric Newberry78e32b02017-04-01 14:34:44 +0000149 tcp::Endpoint endpoint(ip::address::from_string(remoteUri.getHost()),
150 boost::lexical_cast<uint16_t>(remoteUri.getPort()));
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800151
Davide Pesavento46afec42017-05-28 14:28:47 -0400152 // a canonical tcp4/tcp6 FaceUri cannot have a multicast address
153 BOOST_ASSERT(!endpoint.address().is_multicast());
Eric Newberry42602412016-08-27 09:33:18 -0700154
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200155 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
Eric Newberry42602412016-08-27 09:33:18 -0700156 NFD_LOG_TRACE("Requested endpoint is prohibited "
157 "(reserved by NFD or disallowed by face management protocol)");
Eric Newberryf40551a2016-09-05 15:41:16 -0700158 onFailure(406, "Requested endpoint is prohibited");
159 return;
160 }
161
162 if (wantLocalFieldsEnabled && !endpoint.address().is_loopback()) {
163 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
164 onFailure(406, "Local fields can only be enabled on faces with local scope");
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200165 return;
166 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600167
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800168 // very simple logic for now
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200169 for (const auto& i : m_channels) {
170 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
171 (i.first.address().is_v6() && endpoint.address().is_v6())) {
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400172 i.second->connect(endpoint, persistency, wantLocalFieldsEnabled, onCreated, onFailure);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200173 return;
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800174 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200175 }
176
Davide Pesavento46afec42017-05-28 14:28:47 -0400177 NFD_LOG_TRACE("No channels available to connect to " << endpoint);
Eric Newberryf40551a2016-09-05 15:41:16 -0700178 onFailure(504, "No channels available to connect");
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800179}
180
Junxiao Shi38b24c72017-01-05 02:59:31 +0000181void
182TcpFactory::prohibitEndpoint(const tcp::Endpoint& endpoint)
183{
184 if (endpoint.address().is_v4() &&
185 endpoint.address() == ip::address_v4::any()) {
186 prohibitAllIpv4Endpoints(endpoint.port());
187 }
188 else if (endpoint.address().is_v6() &&
189 endpoint.address() == ip::address_v6::any()) {
190 prohibitAllIpv6Endpoints(endpoint.port());
191 }
192
193 NFD_LOG_TRACE("prohibiting TCP " << endpoint);
194 m_prohibitedEndpoints.insert(endpoint);
195}
196
197void
198TcpFactory::prohibitAllIpv4Endpoints(uint16_t port)
199{
200 ///\todo prohibited endpoints need to react to dynamic NIC changes
201 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
202 for (const auto& addr : nic.ipv4Addresses) {
203 if (addr != ip::address_v4::any()) {
204 prohibitEndpoint(tcp::Endpoint(addr, port));
205 }
206 }
207 }
208}
209
210void
211TcpFactory::prohibitAllIpv6Endpoints(uint16_t port)
212{
213 ///\todo prohibited endpoints need to react to dynamic NIC changes
214 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
215 for (const auto& addr : nic.ipv6Addresses) {
216 if (addr != ip::address_v6::any()) {
217 prohibitEndpoint(tcp::Endpoint(addr, port));
218 }
219 }
220 }
221}
222
223shared_ptr<TcpChannel>
224TcpFactory::createChannel(const tcp::Endpoint& endpoint)
225{
226 auto channel = findChannel(endpoint);
227 if (channel)
228 return channel;
229
230 channel = make_shared<TcpChannel>(endpoint);
231 m_channels[endpoint] = channel;
232 prohibitEndpoint(endpoint);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000233 return channel;
234}
235
236shared_ptr<TcpChannel>
237TcpFactory::createChannel(const std::string& localIp, const std::string& localPort)
238{
239 tcp::Endpoint endpoint(ip::address::from_string(localIp),
240 boost::lexical_cast<uint16_t>(localPort));
241 return createChannel(endpoint);
242}
243
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200244std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600245TcpFactory::getChannels() const
246{
Junxiao Shi38b24c72017-01-05 02:59:31 +0000247 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600248}
249
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200250shared_ptr<TcpChannel>
251TcpFactory::findChannel(const tcp::Endpoint& localEndpoint) const
252{
253 auto i = m_channels.find(localEndpoint);
254 if (i != m_channels.end())
255 return i->second;
256 else
257 return nullptr;
258}
259
Junxiao Shi38b24c72017-01-05 02:59:31 +0000260} // namespace face
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800261} // namespace nfd