28 #include <ndn-cxx/net/address-converter.hpp> 33 namespace ip = boost::asio::ip;
41 static std::string id(
"tcp");
65 if (!context.
isDryRun && !m_channels.empty()) {
66 NFD_LOG_WARN(
"Cannot disable tcp4 and tcp6 channels after initialization");
71 bool wantListen =
true;
76 bool isLocalConfigured =
false;
78 for (
const auto& pair : *configSection) {
79 const std::string& key = pair.first;
81 if (key ==
"listen") {
84 else if (key ==
"port") {
85 port = ConfigFile::parseNumber<uint16_t>(pair,
"face_system.tcp");
87 else if (key ==
"enable_v4") {
90 else if (key ==
"enable_v6") {
93 else if (key ==
"local") {
94 isLocalConfigured =
true;
95 for (
const auto& localPair : pair.second) {
96 const std::string& localKey = localPair.first;
97 if (localKey ==
"whitelist") {
100 else if (localKey ==
"blacklist") {
104 BOOST_THROW_EXCEPTION(
ConfigFile::Error(
"Unrecognized option face_system.tcp.local." + localKey));
109 BOOST_THROW_EXCEPTION(
ConfigFile::Error(
"Unrecognized option face_system.tcp." + key));
112 if (!isLocalConfigured) {
113 local.
assign({{
"subnet",
"127.0.0.0/8"}, {
"subnet",
"::1/128"}}, {});
116 if (!enableV4 && !enableV6) {
118 "IPv4 and IPv6 TCP channels have been disabled. Remove face_system.tcp section to disable " 119 "TCP channels or enable at least one channel type."));
127 shared_ptr<TcpChannel> v4Channel = this->
createChannel(endpoint);
128 if (wantListen && !v4Channel->isListening()) {
129 v4Channel->listen(this->
addFace,
nullptr);
134 NFD_LOG_WARN(
"Cannot close tcp4 channel after its creation");
139 shared_ptr<TcpChannel> v6Channel = this->
createChannel(endpoint);
140 if (wantListen && !v6Channel->isListening()) {
141 v6Channel->listen(this->
addFace,
nullptr);
146 NFD_LOG_WARN(
"Cannot close tcp6 channel after its creation");
149 m_local = std::move(local);
161 NFD_LOG_TRACE(
"Cannot create unicast TCP face with LocalUri");
162 onFailure(406,
"Unicast TCP faces cannot be created with a LocalUri");
167 NFD_LOG_TRACE(
"createFace does not support FACE_PERSISTENCY_ON_DEMAND");
168 onFailure(406,
"Outgoing TCP faces do not support on-demand persistency");
176 BOOST_ASSERT(!endpoint.address().is_multicast());
179 NFD_LOG_TRACE(
"createFace cannot create non-local face with local fields enabled");
180 onFailure(406,
"Local fields can only be enabled on faces with local scope");
185 for (
const auto& i : m_channels) {
186 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
187 (i.first.address().is_v6() && endpoint.address().is_v6())) {
188 i.second->connect(endpoint, req.
params, onCreated, onFailure);
193 NFD_LOG_TRACE(
"No channels available to connect to " << endpoint);
194 onFailure(504,
"No channels available to connect");
197 shared_ptr<TcpChannel>
200 auto it = m_channels.find(endpoint);
201 if (it != m_channels.end())
204 auto channel = make_shared<TcpChannel>(endpoint, m_wantCongestionMarking,
205 bind(&TcpFactory::determineFaceScopeFromAddresses,
this, _1, _2));
206 m_channels[endpoint] = channel;
210 std::vector<shared_ptr<const Channel>>
217 TcpFactory::determineFaceScopeFromAddresses(
const boost::asio::ip::address& localAddress,
218 const boost::asio::ip::address& remoteAddress)
const 220 if (m_local(localAddress) && m_local(remoteAddress)) {
std::set< std::string > providedSchemes
FaceUri schemes provided by this ProtocolFactory.
void assign(std::initializer_list< std::pair< std::string, std::string >> whitelist, std::initializer_list< std::pair< std::string, std::string >> blacklist)
bool wantCongestionMarking
const std::string & getHost() const
get host (domain)
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string §ionName)
parse a config option that can be either "yes" or "no"
GeneralConfig generalConfig
void parseWhitelist(const boost::property_tree::ptree &list)
static const std::string & getId()
const std::string & getPort() const
get port
NFD_REGISTER_PROTOCOL_FACTORY(EthernetFactory)
std::function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
std::function< void(const shared_ptr< Face > &face)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
FaceCreatedCallback addFace
callback when a new face is created
#define NFD_LOG_TRACE(expression)
void parseBlacklist(const boost::property_tree::ptree &list)
Provides support for an underlying protocol.
protocol factory for TCP over IPv4 and IPv6
Copyright (c) 2011-2015 Regents of the University of California.
boost::asio::ip::tcp::endpoint Endpoint
void createFace(const CreateFaceRequest &req, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure) override
Try to create a unicast face using the supplied parameters.
TcpFactory(const CtorParams ¶ms)
Parameters to ProtocolFactory constructor.
ndn::nfd::FacePersistency persistency
context for processing a config section in ProtocolFactory
boost::asio::ip::address addressFromString(const std::string &address, boost::system::error_code &ec)
parse and convert the input string into an IP address
#define NFD_LOG_WARN(expression)
std::vector< shared_ptr< const Channel > > getChannels() const override
Encapsulates a face creation request and all its parameters.
optional< FaceUri > localUri
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
Represents a predicate to accept or reject an IP address.
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context) override
process face_system.tcp config section
bool isCanonical() const
determine whether this FaceUri is in canonical form
#define NFD_LOG_INIT(name)
shared_ptr< TcpChannel > createChannel(const tcp::Endpoint &localEndpoint)
Create TCP-based channel using tcp::Endpoint.