32 namespace ip = boost::asio::ip;
40 static std::string id(
"tcp");
57 if (!context.
isDryRun && !m_channels.empty()) {
58 NFD_LOG_WARN(
"Cannot disable tcp4 and tcp6 channels after initialization");
63 bool wantListen =
true;
68 for (
const auto& pair : *configSection) {
69 const std::string& key = pair.first;
71 if (key ==
"listen") {
74 else if (key ==
"port") {
75 port = ConfigFile::parseNumber<uint16_t>(pair,
"face_system.tcp");
77 else if (key ==
"enable_v4") {
80 else if (key ==
"enable_v6") {
84 BOOST_THROW_EXCEPTION(
ConfigFile::Error(
"Unrecognized option face_system.tcp." + key));
88 if (!enableV4 && !enableV6) {
90 "IPv4 and IPv6 TCP channels have been disabled. Remove face_system.tcp section to disable "
91 "TCP channels or enable at least one channel type."));
99 shared_ptr<TcpChannel> v4Channel = this->
createChannel(endpoint);
100 if (wantListen && !v4Channel->isListening()) {
101 v4Channel->listen(context.
addFace,
nullptr);
106 NFD_LOG_WARN(
"Cannot close tcp4 channel after its creation");
111 shared_ptr<TcpChannel> v6Channel = this->
createChannel(endpoint);
112 if (wantListen && !v6Channel->isListening()) {
113 v6Channel->listen(context.
addFace,
nullptr);
118 NFD_LOG_WARN(
"Cannot close tcp6 channel after its creation");
125 ndn::nfd::FacePersistency persistency,
126 bool wantLocalFieldsEnabled,
130 BOOST_ASSERT(uri.isCanonical());
132 if (persistency != ndn::nfd::FACE_PERSISTENCY_PERSISTENT) {
133 NFD_LOG_TRACE(
"createFace only supports FACE_PERSISTENCY_PERSISTENT");
134 onFailure(406,
"Outgoing TCP faces only support persistent persistency");
138 tcp::Endpoint endpoint(ip::address::from_string(uri.getHost()),
139 boost::lexical_cast<uint16_t>(uri.getPort()));
141 if (endpoint.address().is_multicast()) {
143 onFailure(406,
"Cannot create multicast TCP faces");
147 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
149 "(reserved by NFD or disallowed by face management protocol)");
150 onFailure(406,
"Requested endpoint is prohibited");
154 if (wantLocalFieldsEnabled && !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");
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, wantLocalFieldsEnabled, onCreated, onFailure);
169 NFD_LOG_TRACE(
"No channels available to connect to " + boost::lexical_cast<std::string>(endpoint));
170 onFailure(504,
"No channels available to connect");
176 if (endpoint.address().is_v4() &&
177 endpoint.address() == ip::address_v4::any()) {
178 prohibitAllIpv4Endpoints(endpoint.port());
180 else if (endpoint.address().is_v6() &&
181 endpoint.address() == ip::address_v6::any()) {
182 prohibitAllIpv6Endpoints(endpoint.port());
186 m_prohibitedEndpoints.insert(endpoint);
190 TcpFactory::prohibitAllIpv4Endpoints(uint16_t port)
194 for (
const auto& addr : nic.ipv4Addresses) {
195 if (addr != ip::address_v4::any()) {
203 TcpFactory::prohibitAllIpv6Endpoints(uint16_t port)
207 for (
const auto& addr : nic.ipv6Addresses) {
208 if (addr != ip::address_v6::any()) {
215 shared_ptr<TcpChannel>
218 auto channel = findChannel(endpoint);
222 channel = make_shared<TcpChannel>(endpoint);
223 m_channels[endpoint] = channel;
224 prohibitEndpoint(endpoint);
230 shared_ptr<TcpChannel>
234 boost::lexical_cast<uint16_t>(localPort));
238 std::vector<shared_ptr<const Channel>>
244 shared_ptr<TcpChannel>
245 TcpFactory::findChannel(
const tcp::Endpoint& localEndpoint)
const
247 auto i = m_channels.find(localEndpoint);
248 if (i != m_channels.end())
shared_ptr< TcpChannel > createChannel(const tcp::Endpoint &localEndpoint)
Create TCP-based channel using tcp::Endpoint.
#define NFD_LOG_DEBUG(expression)
static const std::string & getId()
std::set< std::string > providedSchemes
FaceUri schemes provided by this ProtocolFactory.
FaceCreatedCallback addFace
std::vector< shared_ptr< const Channel > > getChannels() const override
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"
void createFace(const FaceUri &uri, ndn::nfd::FacePersistency persistency, bool wantLocalFieldsEnabled, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure) override
Try to create Face using the supplied FaceUri.
#define NFD_REGISTER_PROTOCOL_FACTORY(PF)
registers a protocol factory
std::vector< NetworkInterfaceInfo > listNetworkInterfaces()
List configured network interfaces on the system and their info.
#define NFD_LOG_WARN(expression)
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
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context) override
process face_system.tcp config section
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when the face fails to be created.
#define NFD_LOG_INIT(name)
#define NFD_LOG_TRACE(expression)
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when the face is created (as a response to incoming connec...