tcp-factory.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2017, Regents of the University of California,
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.
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/>.
24  */
25 
26 #include "tcp-factory.hpp"
27 
28 #include <ndn-cxx/net/address-converter.hpp>
29 
30 namespace nfd {
31 namespace face {
32 
33 namespace ip = boost::asio::ip;
34 
35 NFD_LOG_INIT("TcpFactory");
37 
38 const std::string&
40 {
41  static std::string id("tcp");
42  return id;
43 }
44 
46  : ProtocolFactory(params)
47 {
48 }
49 
50 void
53 {
54  // 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;
67  }
68 
69  bool wantListen = true;
70  uint16_t port = 6363;
71  bool enableV4 = true;
72  bool enableV6 = true;
73 
74  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));
91  }
92  }
93 
94  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()) {
107  v4Channel->listen(this->addFace, nullptr);
108  }
109  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()) {
119  v6Channel->listen(this->addFace, nullptr);
120  }
121  providedSchemes.insert("tcp6");
122  }
123  else if (providedSchemes.count("tcp6") > 0) {
124  NFD_LOG_WARN("Cannot close tcp6 channel after its creation");
125  }
126  }
127 }
128 
129 void
131  const FaceCreatedCallback& onCreated,
132  const FaceCreationFailedCallback& onFailure)
133 {
134  BOOST_ASSERT(params.remoteUri.isCanonical());
135 
136  if (params.localUri) {
137  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  }
141 
142  if (params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
143  NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
144  onFailure(406, "Outgoing TCP faces do not support on-demand persistency");
145  return;
146  }
147 
148  tcp::Endpoint endpoint(ndn::ip::addressFromString(params.remoteUri.getHost()),
149  boost::lexical_cast<uint16_t>(params.remoteUri.getPort()));
150 
151  // a canonical tcp4/tcp6 FaceUri cannot have a multicast address
152  BOOST_ASSERT(!endpoint.address().is_multicast());
153 
154  if (params.wantLocalFields && !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");
157  return;
158  }
159 
160  // very simple logic for now
161  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())) {
164  i.second->connect(endpoint, params.persistency, params.wantLocalFields,
165  params.wantLpReliability, onCreated, onFailure);
166  return;
167  }
168  }
169 
170  NFD_LOG_TRACE("No channels available to connect to " << endpoint);
171  onFailure(504, "No channels available to connect");
172 }
173 
174 shared_ptr<TcpChannel>
176 {
177  auto it = m_channels.find(endpoint);
178  if (it != m_channels.end())
179  return it->second;
180 
181  auto channel = make_shared<TcpChannel>(endpoint);
182  m_channels[endpoint] = channel;
183  return channel;
184 }
185 
186 std::vector<shared_ptr<const Channel>>
188 {
189  return getChannelsFromMap(m_channels);
190 }
191 
192 } // namespace face
193 } // namespace nfd
FaceCreatedCallback addFace
callback when a new face is created
void createFace(const CreateFaceParams &params, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure) override
Try to create face using the supplied parameters.
shared_ptr< TcpChannel > createChannel(const tcp::Endpoint &localEndpoint)
Create TCP-based channel using tcp::Endpoint.
static const std::string & getId()
Definition: tcp-factory.cpp:39
std::set< std::string > providedSchemes
FaceUri schemes provided by this ProtocolFactory.
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string &sectionName)
parse a config option that can be either "yes" or "no"
Definition: config-file.cpp:59
#define NFD_REGISTER_PROTOCOL_FACTORY(PF)
registers a protocol factory
TcpFactory(const CtorParams &params)
Definition: tcp-factory.cpp:45
#define NFD_LOG_WARN(expression)
Definition: logger.hpp:163
context for processing a config section in ProtocolFactory
Definition: face-system.hpp:85
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
boost::asio::ip::tcp::endpoint Endpoint
Definition: tcp-channel.hpp:35
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context) override
process face_system.tcp config section
Definition: tcp-factory.cpp:51
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
Definition: channel.hpp:44
std::vector< shared_ptr< const Channel > > getChannels() const override
Provides support for an underlying protocol.
Parameters to ProtocolFactory constructor.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:122
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:160
Parameters to ProtocolFactory::createFace.
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Definition: channel.hpp:35