blob: 1546d24d4e1b62403954b6438c0824abb57c55b1 [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
Yanbiao Liafb20592017-08-03 16:20:46 -070028#include <ndn-cxx/net/address-converter.hpp>
29
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080030namespace nfd {
Junxiao Shi38b24c72017-01-05 02:59:31 +000031namespace face {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080032
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020033namespace ip = boost::asio::ip;
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080034
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020035NFD_LOG_INIT("TcpFactory");
Junxiao Shib47247d2017-01-24 15:09:16 +000036NFD_REGISTER_PROTOCOL_FACTORY(TcpFactory);
37
38const std::string&
39TcpFactory::getId()
40{
41 static std::string id("tcp");
42 return id;
43}
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060044
Junxiao Shi0ba6d642017-07-17 00:53:22 +000045TcpFactory::TcpFactory(const CtorParams& params)
46 : ProtocolFactory(params)
47{
48}
49
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060050void
Junxiao Shi38b24c72017-01-05 02:59:31 +000051TcpFactory::processConfig(OptionalConfigSection configSection,
52 FaceSystem::ConfigContext& context)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060053{
Junxiao Shi38b24c72017-01-05 02:59:31 +000054 // tcp
55 // {
56 // listen yes
57 // port 6363
58 // enable_v4 yes
59 // enable_v6 yes
60 // }
61
62 if (!configSection) {
63 if (!context.isDryRun && !m_channels.empty()) {
64 NFD_LOG_WARN("Cannot disable tcp4 and tcp6 channels after initialization");
65 }
66 return;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020067 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060068
Junxiao Shi38b24c72017-01-05 02:59:31 +000069 bool wantListen = true;
70 uint16_t port = 6363;
71 bool enableV4 = true;
72 bool enableV6 = true;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060073
Junxiao Shi38b24c72017-01-05 02:59:31 +000074 for (const auto& pair : *configSection) {
75 const std::string& key = pair.first;
76
77 if (key == "listen") {
78 wantListen = ConfigFile::parseYesNo(pair, "face_system.tcp");
79 }
80 else if (key == "port") {
81 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.tcp");
82 }
83 else if (key == "enable_v4") {
84 enableV4 = ConfigFile::parseYesNo(pair, "face_system.tcp");
85 }
86 else if (key == "enable_v6") {
87 enableV6 = ConfigFile::parseYesNo(pair, "face_system.tcp");
88 }
89 else {
90 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.tcp." + key));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060091 }
Davide Pesaventob499a602014-11-18 22:36:56 +010092 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060093
Junxiao Shi38b24c72017-01-05 02:59:31 +000094 if (!enableV4 && !enableV6) {
95 BOOST_THROW_EXCEPTION(ConfigFile::Error(
96 "IPv4 and IPv6 TCP channels have been disabled. Remove face_system.tcp section to disable "
97 "TCP channels or enable at least one channel type."));
98 }
99
100 if (!context.isDryRun) {
101 providedSchemes.insert("tcp");
102
103 if (enableV4) {
104 tcp::Endpoint endpoint(ip::tcp::v4(), port);
105 shared_ptr<TcpChannel> v4Channel = this->createChannel(endpoint);
106 if (wantListen && !v4Channel->isListening()) {
Junxiao Shi2d491752017-07-14 21:32:05 +0000107 v4Channel->listen(this->addFace, nullptr);
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800108 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000109 providedSchemes.insert("tcp4");
110 }
111 else if (providedSchemes.count("tcp4") > 0) {
112 NFD_LOG_WARN("Cannot close tcp4 channel after its creation");
113 }
114
115 if (enableV6) {
116 tcp::Endpoint endpoint(ip::tcp::v6(), port);
117 shared_ptr<TcpChannel> v6Channel = this->createChannel(endpoint);
118 if (wantListen && !v6Channel->isListening()) {
Junxiao Shi2d491752017-07-14 21:32:05 +0000119 v6Channel->listen(this->addFace, nullptr);
Junxiao Shi38b24c72017-01-05 02:59:31 +0000120 }
121 providedSchemes.insert("tcp6");
122 }
123 else if (providedSchemes.count("tcp6") > 0) {
124 NFD_LOG_WARN("Cannot close tcp6 channel after its creation");
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600125 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100126 }
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800127}
128
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800129void
Eric Newberry944f38b2017-07-20 20:54:22 -0400130TcpFactory::createFace(const CreateFaceParams& params,
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800131 const FaceCreatedCallback& onCreated,
Eric Newberryf40551a2016-09-05 15:41:16 -0700132 const FaceCreationFailedCallback& onFailure)
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800133{
Eric Newberry944f38b2017-07-20 20:54:22 -0400134 BOOST_ASSERT(params.remoteUri.isCanonical());
Eric Newberry78e32b02017-04-01 14:34:44 +0000135
Eric Newberry944f38b2017-07-20 20:54:22 -0400136 if (params.localUri) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000137 NFD_LOG_TRACE("Cannot create unicast TCP face with LocalUri");
138 onFailure(406, "Unicast TCP faces cannot be created with a LocalUri");
139 return;
140 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200141
Eric Newberry944f38b2017-07-20 20:54:22 -0400142 if (params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400143 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
144 onFailure(406, "Outgoing TCP faces do not support on-demand persistency");
Eric Newberry42602412016-08-27 09:33:18 -0700145 return;
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800146 }
147
Yanbiao Liafb20592017-08-03 16:20:46 -0700148 tcp::Endpoint endpoint(ndn::ip::addressFromString(params.remoteUri.getHost()),
Eric Newberry944f38b2017-07-20 20:54:22 -0400149 boost::lexical_cast<uint16_t>(params.remoteUri.getPort()));
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800150
Davide Pesavento46afec42017-05-28 14:28:47 -0400151 // a canonical tcp4/tcp6 FaceUri cannot have a multicast address
152 BOOST_ASSERT(!endpoint.address().is_multicast());
Eric Newberry42602412016-08-27 09:33:18 -0700153
Eric Newberry2642cd22017-07-13 21:34:53 -0400154 if (params.wantLocalFields && !endpoint.address().is_loopback()) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700155 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 Newberry2642cd22017-07-13 21:34:53 -0400164 i.second->connect(endpoint, params.persistency, params.wantLocalFields,
165 params.wantLpReliability, onCreated, onFailure);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200166 return;
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800167 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200168 }
169
Davide Pesavento46afec42017-05-28 14:28:47 -0400170 NFD_LOG_TRACE("No channels available to connect to " << endpoint);
Eric Newberryf40551a2016-09-05 15:41:16 -0700171 onFailure(504, "No channels available to connect");
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800172}
173
Junxiao Shi38b24c72017-01-05 02:59:31 +0000174shared_ptr<TcpChannel>
175TcpFactory::createChannel(const tcp::Endpoint& endpoint)
176{
177 auto channel = findChannel(endpoint);
178 if (channel)
179 return channel;
180
181 channel = make_shared<TcpChannel>(endpoint);
182 m_channels[endpoint] = channel;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000183 return channel;
184}
185
186shared_ptr<TcpChannel>
187TcpFactory::createChannel(const std::string& localIp, const std::string& localPort)
188{
Yanbiao Liafb20592017-08-03 16:20:46 -0700189 tcp::Endpoint endpoint(ndn::ip::addressFromString(localIp),
Junxiao Shi38b24c72017-01-05 02:59:31 +0000190 boost::lexical_cast<uint16_t>(localPort));
191 return createChannel(endpoint);
192}
193
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200194std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600195TcpFactory::getChannels() const
196{
Junxiao Shi38b24c72017-01-05 02:59:31 +0000197 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600198}
199
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200200shared_ptr<TcpChannel>
201TcpFactory::findChannel(const tcp::Endpoint& localEndpoint) const
202{
203 auto i = m_channels.find(localEndpoint);
204 if (i != m_channels.end())
205 return i->second;
206 else
207 return nullptr;
208}
209
Junxiao Shi38b24c72017-01-05 02:59:31 +0000210} // namespace face
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800211} // namespace nfd