NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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  const FaceCreatedCallback& onCreated,
103  const FaceCreationFailedCallback& onConnectFailed)
104 {
105  BOOST_ASSERT(uri.isCanonical());
106 
107  if (persistency != ndn::nfd::FACE_PERSISTENCY_PERSISTENT) {
108  BOOST_THROW_EXCEPTION(Error("TcpFactory::createFace supports only FACE_PERSISTENCY_PERSISTENT"));
109  }
110 
111  tcp::Endpoint endpoint(ip::address::from_string(uri.getHost()),
112  boost::lexical_cast<uint16_t>(uri.getPort()));
113 
114  if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
115  onConnectFailed("Requested endpoint is prohibited "
116  "(reserved by this NFD or disallowed by face management protocol)");
117  return;
118  }
119 
120  // very simple logic for now
121  for (const auto& i : m_channels) {
122  if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
123  (i.first.address().is_v6() && endpoint.address().is_v6())) {
124  i.second->connect(endpoint, onCreated, onConnectFailed);
125  return;
126  }
127  }
128 
129  onConnectFailed("No channels available to connect to " + boost::lexical_cast<std::string>(endpoint));
130 }
131 
132 std::vector<shared_ptr<const Channel>>
134 {
135  std::vector<shared_ptr<const Channel>> channels;
136  channels.reserve(m_channels.size());
137 
138  for (const auto& i : m_channels)
139  channels.push_back(i.second);
140 
141  return channels;
142 }
143 
144 shared_ptr<TcpChannel>
145 TcpFactory::findChannel(const tcp::Endpoint& localEndpoint) const
146 {
147  auto i = m_channels.find(localEndpoint);
148  if (i != m_channels.end())
149  return i->second;
150  else
151  return nullptr;
152 }
153 
154 } // namespace nfd
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:55
function< void(const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when the face fails to be created.
Definition: channel.hpp:44
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
Exception of TcpFactory.
Definition: tcp-factory.hpp:40
bool isCanonical() const
determine whether this FaceUri is in canonical form
Definition: face-uri.cpp:507
contains information about a network interface
virtual void createFace(const FaceUri &uri, ndn::nfd::FacePersistency persistency, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onConnectFailed) 1
Try to create Face using the supplied FaceUri.
std::vector< NetworkInterfaceInfo > listNetworkInterfaces()
List configured network interfaces on the system and their info.
virtual std::vector< shared_ptr< const Channel > > getChannels() const 1
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:123
boost::asio::ip::tcp::endpoint Endpoint
Definition: tcp-channel.hpp:35
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:54
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:116