31 namespace ip = boost::asio::ip;
39 static std::string id(
"tcp");
63 if (!context.
isDryRun && !m_channels.empty()) {
64 NFD_LOG_WARN(
"Cannot disable tcp4 and tcp6 channels after initialization");
69 bool wantListen =
true;
74 bool isLocalConfigured =
false;
76 for (
const auto& pair : *configSection) {
77 const std::string& key = pair.first;
79 if (key ==
"listen") {
82 else if (key ==
"port") {
83 port = ConfigFile::parseNumber<uint16_t>(pair,
"face_system.tcp");
85 else if (key ==
"enable_v4") {
88 else if (key ==
"enable_v6") {
91 else if (key ==
"local") {
92 isLocalConfigured =
true;
93 for (
const auto& localPair : pair.second) {
94 const std::string& localKey = localPair.first;
95 if (localKey ==
"whitelist") {
98 else if (localKey ==
"blacklist") {
102 BOOST_THROW_EXCEPTION(
ConfigFile::Error(
"Unrecognized option face_system.tcp.local." + localKey));
107 BOOST_THROW_EXCEPTION(
ConfigFile::Error(
"Unrecognized option face_system.tcp." + key));
110 if (!isLocalConfigured) {
111 local.
assign({{
"subnet",
"127.0.0.0/8"}, {
"subnet",
"::1/128"}}, {});
114 if (!enableV4 && !enableV6) {
116 "IPv4 and IPv6 TCP channels have been disabled. Remove face_system.tcp section to disable " 117 "TCP channels or enable at least one channel type."));
125 shared_ptr<TcpChannel> v4Channel = this->
createChannel(endpoint);
126 if (wantListen && !v4Channel->isListening()) {
127 v4Channel->listen(this->
addFace,
nullptr);
132 NFD_LOG_WARN(
"Cannot close tcp4 channel after its creation");
137 shared_ptr<TcpChannel> v6Channel = this->
createChannel(endpoint);
138 if (wantListen && !v6Channel->isListening()) {
139 v6Channel->listen(this->
addFace,
nullptr);
144 NFD_LOG_WARN(
"Cannot close tcp6 channel after its creation");
147 m_local = std::move(local);
156 BOOST_ASSERT(req.
remoteUri.isCanonical());
159 NFD_LOG_TRACE(
"Cannot create unicast TCP face with LocalUri");
160 onFailure(406,
"Unicast TCP faces cannot be created with a LocalUri");
165 NFD_LOG_TRACE(
"createFace does not support FACE_PERSISTENCY_ON_DEMAND");
166 onFailure(406,
"Outgoing TCP faces do not support on-demand persistency");
171 boost::lexical_cast<uint16_t>(req.
remoteUri.getPort()));
174 BOOST_ASSERT(!endpoint.address().is_multicast());
177 NFD_LOG_TRACE(
"createFace cannot create non-local face with local fields enabled");
178 onFailure(406,
"Local fields can only be enabled on faces with local scope");
183 NFD_LOG_TRACE(
"createFace cannot create a TCP face with an overridden MTU");
184 onFailure(406,
"TCP faces do not support MTU overrides");
189 for (
const auto& i : m_channels) {
190 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
191 (i.first.address().is_v6() && endpoint.address().is_v6())) {
192 i.second->connect(endpoint, req.
params, onCreated, onFailure);
197 NFD_LOG_TRACE(
"No channels available to connect to " << endpoint);
198 onFailure(504,
"No channels available to connect");
201 shared_ptr<TcpChannel>
204 auto it = m_channels.find(endpoint);
205 if (it != m_channels.end())
208 auto channel = make_shared<TcpChannel>(endpoint, m_wantCongestionMarking,
209 bind(&TcpFactory::determineFaceScopeFromAddresses,
this, _1, _2));
210 m_channels[endpoint] = channel;
214 std::vector<shared_ptr<const Channel>>
221 TcpFactory::determineFaceScopeFromAddresses(
const boost::asio::ip::address& localAddress,
222 const boost::asio::ip::address& remoteAddress)
const 224 if (m_local(localAddress) && m_local(remoteAddress)) {
225 return ndn::nfd::FACE_SCOPE_LOCAL;
227 return ndn::nfd::FACE_SCOPE_NON_LOCAL;
FaceCreatedCallback addFace
callback when a new face is created
void parseWhitelist(const boost::property_tree::ptree &list)
shared_ptr< TcpChannel > createChannel(const tcp::Endpoint &localEndpoint)
Create TCP-based channel using tcp::Endpoint.
static const std::string & getId()
void createFace(const CreateFaceRequest &req, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure) override
Try to create a unicast face using the supplied parameters.
optional< FaceUri > localUri
std::set< std::string > providedSchemes
FaceUri schemes provided by this ProtocolFactory.
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.
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"
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...
#define NFD_REGISTER_PROTOCOL_FACTORY(PF)
registers a protocol factory
TcpFactory(const CtorParams ¶ms)
context for processing a config section in ProtocolFactory
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...
Represents a predicate to accept or reject an IP address.
boost::asio::ip::tcp::endpoint Endpoint
GeneralConfig generalConfig
void assign(std::initializer_list< std::pair< std::string, std::string >> whitelist, std::initializer_list< std::pair< std::string, std::string >> blacklist)
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context) override
process face_system.tcp config section
ndn::nfd::FacePersistency persistency
void parseBlacklist(const boost::property_tree::ptree &list)
std::vector< shared_ptr< const Channel > > getChannels() const override
Provides support for an underlying protocol.
Parameters to ProtocolFactory constructor.
#define NFD_LOG_INIT(name)
bool wantCongestionMarking
Encapsulates a face creation request and all its parameters.