NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
tcp-factory.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "tcp-factory.hpp"
27 #include "core/logger.hpp"
29 
30 namespace nfd {
31 
32 namespace ip = boost::asio::ip;
33 
34 NFD_LOG_INIT("TcpFactory");
35 
36 void
37 TcpFactory::prohibitEndpoint(const tcp::Endpoint& endpoint)
38 {
39  if (endpoint.address().is_v4() &&
40  endpoint.address() == ip::address_v4::any()) {
41  prohibitAllIpv4Endpoints(endpoint.port());
42  }
43  else if (endpoint.address().is_v6() &&
44  endpoint.address() == ip::address_v6::any()) {
45  prohibitAllIpv6Endpoints(endpoint.port());
46  }
47 
48  NFD_LOG_TRACE("prohibiting TCP " << endpoint);
49  m_prohibitedEndpoints.insert(endpoint);
50 }
51 
52 void
53 TcpFactory::prohibitAllIpv4Endpoints(uint16_t port)
54 {
55  for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
56  for (const auto& addr : nic.ipv4Addresses) {
57  if (addr != ip::address_v4::any()) {
58  prohibitEndpoint(tcp::Endpoint(addr, port));
59  }
60  }
61  }
62 }
63 
64 void
65 TcpFactory::prohibitAllIpv6Endpoints(uint16_t port)
66 {
67  for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
68  for (const auto& addr : nic.ipv6Addresses) {
69  if (addr != ip::address_v6::any()) {
70  prohibitEndpoint(tcp::Endpoint(addr, port));
71  }
72  }
73  }
74 }
75 
76 shared_ptr<TcpChannel>
78 {
79  auto channel = findChannel(endpoint);
80  if (channel)
81  return channel;
82 
83  channel = make_shared<TcpChannel>(endpoint);
84  m_channels[endpoint] = channel;
85  prohibitEndpoint(endpoint);
86 
87  NFD_LOG_DEBUG("Channel [" << endpoint << "] created");
88  return channel;
89 }
90 
91 shared_ptr<TcpChannel>
92 TcpFactory::createChannel(const std::string& localIp, const std::string& localPort)
93 {
94  tcp::Endpoint endpoint(ip::address::from_string(localIp),
95  boost::lexical_cast<uint16_t>(localPort));
96  return createChannel(endpoint);
97 }
98 
99 void
101  ndn::nfd::FacePersistency persistency,
102  bool wantLocalFieldsEnabled,
103  const FaceCreatedCallback& onCreated,
104  const FaceCreationFailedCallback& onFailure)
105 {
106  BOOST_ASSERT(uri.isCanonical());
107 
108  if (persistency != ndn::nfd::FACE_PERSISTENCY_PERSISTENT) {
109  NFD_LOG_TRACE("createFace only supports FACE_PERSISTENCY_PERSISTENT");
110  onFailure(406, "Outgoing TCP faces only support persistent persistency");
111  return;
112  }
113 
114  tcp::Endpoint endpoint(ip::address::from_string(uri.getHost()),
115  boost::lexical_cast<uint16_t>(uri.getPort()));
116 
117  if (endpoint.address().is_multicast()) {
118  NFD_LOG_TRACE("createFace cannot create multicast faces");
119  onFailure(406, "Cannot create multicast TCP faces");
120  return;
121  }
122 
123  if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
124  NFD_LOG_TRACE("Requested endpoint is prohibited "
125  "(reserved by NFD or disallowed by face management protocol)");
126  onFailure(406, "Requested endpoint is prohibited");
127  return;
128  }
129 
130  if (wantLocalFieldsEnabled && !endpoint.address().is_loopback()) {
131  NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
132  onFailure(406, "Local fields can only be enabled on faces with local scope");
133  return;
134  }
135 
136  // very simple logic for now
137  for (const auto& i : m_channels) {
138  if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
139  (i.first.address().is_v6() && endpoint.address().is_v6())) {
140  i.second->connect(endpoint, wantLocalFieldsEnabled, onCreated, onFailure);
141  return;
142  }
143  }
144 
145  NFD_LOG_TRACE("No channels available to connect to " + boost::lexical_cast<std::string>(endpoint));
146  onFailure(504, "No channels available to connect");
147 }
148 
149 std::vector<shared_ptr<const Channel>>
151 {
152  std::vector<shared_ptr<const Channel>> channels;
153  channels.reserve(m_channels.size());
154 
155  for (const auto& i : m_channels)
156  channels.push_back(i.second);
157 
158  return channels;
159 }
160 
161 shared_ptr<TcpChannel>
162 TcpFactory::findChannel(const tcp::Endpoint& localEndpoint) const
163 {
164  auto i = m_channels.find(localEndpoint);
165  if (i != m_channels.end())
166  return i->second;
167  else
168  return nullptr;
169 }
170 
171 } // namespace nfd
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
bool isCanonical() const
determine whether this FaceUri is in canonical form
Definition: face-uri.cpp:552
contains information about a network interface
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:55
std::vector< NetworkInterfaceInfo > listNetworkInterfaces()
List configured network interfaces on the system and their info.
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:54
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
const std::string & getPort() const
get port
Definition: face-uri.hpp:127
boost::asio::ip::tcp::endpoint Endpoint
Definition: tcp-channel.hpp:35
virtual 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.
function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when the face fails to be created.
Definition: channel.hpp:44
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...
Definition: channel.hpp:38
shared_ptr< TcpChannel > createChannel(const tcp::Endpoint &localEndpoint)
Create TCP-based channel using tcp::Endpoint.
Definition: tcp-factory.cpp:77
const std::string & getHost() const
get host (domain)
Definition: face-uri.hpp:120
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
virtual std::vector< shared_ptr< const Channel > > getChannels() const override