31 namespace ip = boost::asio::ip;
39 static std::string id(
"tcp");
58 if (!context.
isDryRun && !m_channels.empty()) {
59 NFD_LOG_WARN(
"Cannot disable TCP channels after initialization");
64 bool wantListen =
true;
69 bool isLocalConfigured =
false;
71 for (
const auto& pair : *configSection) {
72 const std::string& key = pair.first;
74 if (key ==
"listen") {
77 else if (key ==
"port") {
78 port = ConfigFile::parseNumber<uint16_t>(pair,
"face_system.tcp");
80 else if (key ==
"enable_v4") {
83 else if (key ==
"enable_v6") {
86 else if (key ==
"local") {
87 isLocalConfigured =
true;
88 for (
const auto& localPair : pair.second) {
89 const std::string& localKey = localPair.first;
90 if (localKey ==
"whitelist") {
93 else if (localKey ==
"blacklist") {
97 NDN_THROW(
ConfigFile::Error(
"Unrecognized option face_system.tcp.local." + localKey));
105 if (!isLocalConfigured) {
106 local.
assign({{
"subnet",
"127.0.0.0/8"}, {
"subnet",
"::1/128"}}, {});
109 if (!enableV4 && !enableV6) {
111 "IPv4 and IPv6 TCP channels have been disabled. Remove face_system.tcp section to disable " 112 "TCP channels or enable at least one channel type."));
124 if (wantListen && !v4Channel->isListening()) {
125 v4Channel->listen(this->
addFace,
nullptr);
130 NFD_LOG_WARN(
"Cannot close tcp4 channel after its creation");
136 if (wantListen && !v6Channel->isListening()) {
137 v6Channel->listen(this->
addFace,
nullptr);
142 NFD_LOG_WARN(
"Cannot close tcp6 channel after its creation");
145 m_local = std::move(local);
154 NFD_LOG_TRACE(
"Cannot create unicast TCP face with LocalUri");
155 onFailure(406,
"Unicast TCP faces cannot be created with a LocalUri");
160 NFD_LOG_TRACE(
"createFace does not support FACE_PERSISTENCY_ON_DEMAND");
161 onFailure(406,
"Outgoing TCP faces do not support on-demand persistency");
166 boost::lexical_cast<uint16_t>(req.
remoteUri.getPort()));
169 BOOST_ASSERT(!endpoint.address().is_multicast());
172 NFD_LOG_TRACE(
"createFace cannot create non-local face with local fields enabled");
173 onFailure(406,
"Local fields can only be enabled on faces with local scope");
178 NFD_LOG_TRACE(
"createFace cannot create a TCP face with an overridden MTU");
179 onFailure(406,
"TCP faces do not support MTU overrides");
184 for (
const auto& i : m_channels) {
185 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
186 (i.first.address().is_v6() && endpoint.address().is_v6())) {
187 i.second->connect(endpoint, req.
params, onCreated, onFailure);
192 NFD_LOG_TRACE(
"No channels available to connect to " << endpoint);
193 onFailure(504,
"No channels available to connect");
196 shared_ptr<TcpChannel>
199 auto it = m_channels.find(endpoint);
200 if (it != m_channels.end())
203 auto channel = make_shared<TcpChannel>(endpoint, m_wantCongestionMarking,
204 bind(&TcpFactory::determineFaceScopeFromAddresses,
this, _1, _2));
205 m_channels[endpoint] = channel;
209 std::vector<shared_ptr<const Channel>>
210 TcpFactory::doGetChannels()
const 216 TcpFactory::determineFaceScopeFromAddresses(
const boost::asio::ip::address& localAddress,
217 const boost::asio::ip::address& remoteAddress)
const 219 if (m_local(localAddress) && m_local(remoteAddress)) {
220 return ndn::nfd::FACE_SCOPE_LOCAL;
222 return ndn::nfd::FACE_SCOPE_NON_LOCAL;
FaceCreatedCallback addFace
callback when a new face is created
shared_ptr< TcpChannel > createChannel(const tcp::Endpoint &localEndpoint)
Create TCP-based channel using tcp::Endpoint.
void parseWhitelist(const boost::property_tree::ptree &list)
void parseBlacklist(const boost::property_tree::ptree &list)
Represents a predicate to accept or reject an IP address.
optional< FaceUri > localUri
std::set< std::string > providedSchemes
FaceUri schemes provided by this protocol factory.
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
#define NFD_REGISTER_PROTOCOL_FACTORY(PF)
Registers a protocol factory.
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...
boost::asio::ip::tcp::endpoint Endpoint
GeneralConfig generalConfig
static const std::string & getId() noexcept
ndn::nfd::FacePersistency persistency
#define NFD_LOG_INIT(name)
void assign(std::initializer_list< std::pair< std::string, std::string >> whitelist, std::initializer_list< std::pair< std::string, std::string >> blacklist)
bool wantCongestionMarking
std::function< void(const shared_ptr< Face > &)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Encapsulates a face creation request and all its parameters.