blob: a273e428270c3a088b125db557ec15761b778376 [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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 Afanasyev5f1ec252014-02-28 10:59:17 -080027#include "core/logger.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080028
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080029namespace nfd {
Junxiao Shi38b24c72017-01-05 02:59:31 +000030namespace face {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080031
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020032namespace ip = boost::asio::ip;
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080033
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020034NFD_LOG_INIT("TcpFactory");
Junxiao Shib47247d2017-01-24 15:09:16 +000035NFD_REGISTER_PROTOCOL_FACTORY(TcpFactory);
36
37const std::string&
38TcpFactory::getId()
39{
40 static std::string id("tcp");
41 return id;
42}
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060043
44void
Junxiao Shi38b24c72017-01-05 02:59:31 +000045TcpFactory::processConfig(OptionalConfigSection configSection,
46 FaceSystem::ConfigContext& context)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060047{
Junxiao Shi38b24c72017-01-05 02:59:31 +000048 // tcp
49 // {
50 // listen yes
51 // port 6363
52 // enable_v4 yes
53 // enable_v6 yes
54 // }
55
56 if (!configSection) {
57 if (!context.isDryRun && !m_channels.empty()) {
58 NFD_LOG_WARN("Cannot disable tcp4 and tcp6 channels after initialization");
59 }
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;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060067
Junxiao Shi38b24c72017-01-05 02:59:31 +000068 for (const auto& pair : *configSection) {
69 const std::string& key = pair.first;
70
71 if (key == "listen") {
72 wantListen = ConfigFile::parseYesNo(pair, "face_system.tcp");
73 }
74 else if (key == "port") {
75 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.tcp");
76 }
77 else if (key == "enable_v4") {
78 enableV4 = ConfigFile::parseYesNo(pair, "face_system.tcp");
79 }
80 else if (key == "enable_v6") {
81 enableV6 = ConfigFile::parseYesNo(pair, "face_system.tcp");
82 }
83 else {
84 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.tcp." + key));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060085 }
Davide Pesaventob499a602014-11-18 22:36:56 +010086 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060087
Junxiao Shi38b24c72017-01-05 02:59:31 +000088 if (!enableV4 && !enableV6) {
89 BOOST_THROW_EXCEPTION(ConfigFile::Error(
90 "IPv4 and IPv6 TCP channels have been disabled. Remove face_system.tcp section to disable "
91 "TCP channels or enable at least one channel type."));
92 }
93
94 if (!context.isDryRun) {
95 providedSchemes.insert("tcp");
96
97 if (enableV4) {
98 tcp::Endpoint endpoint(ip::tcp::v4(), port);
99 shared_ptr<TcpChannel> v4Channel = this->createChannel(endpoint);
100 if (wantListen && !v4Channel->isListening()) {
101 v4Channel->listen(context.addFace, nullptr);
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800102 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000103 providedSchemes.insert("tcp4");
104 }
105 else if (providedSchemes.count("tcp4") > 0) {
106 NFD_LOG_WARN("Cannot close tcp4 channel after its creation");
107 }
108
109 if (enableV6) {
110 tcp::Endpoint endpoint(ip::tcp::v6(), port);
111 shared_ptr<TcpChannel> v6Channel = this->createChannel(endpoint);
112 if (wantListen && !v6Channel->isListening()) {
113 v6Channel->listen(context.addFace, nullptr);
114 }
115 providedSchemes.insert("tcp6");
116 }
117 else if (providedSchemes.count("tcp6") > 0) {
118 NFD_LOG_WARN("Cannot close tcp6 channel after its creation");
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600119 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100120 }
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800121}
122
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800123void
124TcpFactory::createFace(const FaceUri& uri,
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800125 ndn::nfd::FacePersistency persistency,
Eric Newberryf40551a2016-09-05 15:41:16 -0700126 bool wantLocalFieldsEnabled,
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800127 const FaceCreatedCallback& onCreated,
Eric Newberryf40551a2016-09-05 15:41:16 -0700128 const FaceCreationFailedCallback& onFailure)
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800129{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200130 BOOST_ASSERT(uri.isCanonical());
131
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800132 if (persistency != ndn::nfd::FACE_PERSISTENCY_PERSISTENT) {
Eric Newberry42602412016-08-27 09:33:18 -0700133 NFD_LOG_TRACE("createFace only supports FACE_PERSISTENCY_PERSISTENT");
Eric Newberryf40551a2016-09-05 15:41:16 -0700134 onFailure(406, "Outgoing TCP faces only support persistent persistency");
Eric Newberry42602412016-08-27 09:33:18 -0700135 return;
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800136 }
137
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200138 tcp::Endpoint endpoint(ip::address::from_string(uri.getHost()),
139 boost::lexical_cast<uint16_t>(uri.getPort()));
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800140
Eric Newberry42602412016-08-27 09:33:18 -0700141 if (endpoint.address().is_multicast()) {
142 NFD_LOG_TRACE("createFace cannot create multicast faces");
Eric Newberryf40551a2016-09-05 15:41:16 -0700143 onFailure(406, "Cannot create multicast TCP faces");
Eric Newberry42602412016-08-27 09:33:18 -0700144 return;
145 }
146
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200147 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
Eric Newberry42602412016-08-27 09:33:18 -0700148 NFD_LOG_TRACE("Requested endpoint is prohibited "
149 "(reserved by NFD or disallowed by face management protocol)");
Eric Newberryf40551a2016-09-05 15:41:16 -0700150 onFailure(406, "Requested endpoint is prohibited");
151 return;
152 }
153
154 if (wantLocalFieldsEnabled && !endpoint.address().is_loopback()) {
155 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
156 onFailure(406, "Local fields can only be enabled on faces with local scope");
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200157 return;
158 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600159
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800160 // very simple logic for now
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200161 for (const auto& i : m_channels) {
162 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
163 (i.first.address().is_v6() && endpoint.address().is_v6())) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700164 i.second->connect(endpoint, wantLocalFieldsEnabled, onCreated, onFailure);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200165 return;
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800166 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200167 }
168
Eric Newberry42602412016-08-27 09:33:18 -0700169 NFD_LOG_TRACE("No channels available to connect to " + boost::lexical_cast<std::string>(endpoint));
Eric Newberryf40551a2016-09-05 15:41:16 -0700170 onFailure(504, "No channels available to connect");
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800171}
172
Junxiao Shi38b24c72017-01-05 02:59:31 +0000173void
174TcpFactory::prohibitEndpoint(const tcp::Endpoint& endpoint)
175{
176 if (endpoint.address().is_v4() &&
177 endpoint.address() == ip::address_v4::any()) {
178 prohibitAllIpv4Endpoints(endpoint.port());
179 }
180 else if (endpoint.address().is_v6() &&
181 endpoint.address() == ip::address_v6::any()) {
182 prohibitAllIpv6Endpoints(endpoint.port());
183 }
184
185 NFD_LOG_TRACE("prohibiting TCP " << endpoint);
186 m_prohibitedEndpoints.insert(endpoint);
187}
188
189void
190TcpFactory::prohibitAllIpv4Endpoints(uint16_t port)
191{
192 ///\todo prohibited endpoints need to react to dynamic NIC changes
193 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
194 for (const auto& addr : nic.ipv4Addresses) {
195 if (addr != ip::address_v4::any()) {
196 prohibitEndpoint(tcp::Endpoint(addr, port));
197 }
198 }
199 }
200}
201
202void
203TcpFactory::prohibitAllIpv6Endpoints(uint16_t port)
204{
205 ///\todo prohibited endpoints need to react to dynamic NIC changes
206 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
207 for (const auto& addr : nic.ipv6Addresses) {
208 if (addr != ip::address_v6::any()) {
209 prohibitEndpoint(tcp::Endpoint(addr, port));
210 }
211 }
212 }
213}
214
215shared_ptr<TcpChannel>
216TcpFactory::createChannel(const tcp::Endpoint& endpoint)
217{
218 auto channel = findChannel(endpoint);
219 if (channel)
220 return channel;
221
222 channel = make_shared<TcpChannel>(endpoint);
223 m_channels[endpoint] = channel;
224 prohibitEndpoint(endpoint);
225
226 NFD_LOG_DEBUG("Channel [" << endpoint << "] created");
227 return channel;
228}
229
230shared_ptr<TcpChannel>
231TcpFactory::createChannel(const std::string& localIp, const std::string& localPort)
232{
233 tcp::Endpoint endpoint(ip::address::from_string(localIp),
234 boost::lexical_cast<uint16_t>(localPort));
235 return createChannel(endpoint);
236}
237
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200238std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600239TcpFactory::getChannels() const
240{
Junxiao Shi38b24c72017-01-05 02:59:31 +0000241 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600242}
243
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200244shared_ptr<TcpChannel>
245TcpFactory::findChannel(const tcp::Endpoint& localEndpoint) const
246{
247 auto i = m_channels.find(localEndpoint);
248 if (i != m_channels.end())
249 return i->second;
250 else
251 return nullptr;
252}
253
Junxiao Shi38b24c72017-01-05 02:59:31 +0000254} // namespace face
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800255} // namespace nfd