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 #include "core/global-io.hpp"
30 
31 NFD_LOG_INIT("TcpFactory");
32 
33 namespace nfd {
34 
35 static const boost::asio::ip::address_v4 ALL_V4_ENDPOINT(
36  boost::asio::ip::address_v4::from_string("0.0.0.0"));
37 
38 static const boost::asio::ip::address_v6 ALL_V6_ENDPOINT(
39  boost::asio::ip::address_v6::from_string("::"));
40 
41 TcpFactory::TcpFactory(const std::string& defaultPort/* = "6363"*/)
42  : m_defaultPort(defaultPort)
43 {
44 }
45 
46 void
47 TcpFactory::prohibitEndpoint(const tcp::Endpoint& endpoint)
48 {
49  using namespace boost::asio::ip;
50 
51  const address& address = endpoint.address();
52 
53  if (address.is_v4() && address == ALL_V4_ENDPOINT)
54  {
55  prohibitAllIpv4Endpoints(endpoint.port());
56  }
57  else if (endpoint.address().is_v6() && address == ALL_V6_ENDPOINT)
58  {
59  prohibitAllIpv6Endpoints(endpoint.port());
60  }
61 
62  NFD_LOG_TRACE("prohibiting TCP " << endpoint);
63 
64  m_prohibitedEndpoints.insert(endpoint);
65 }
66 
67 void
68 TcpFactory::prohibitAllIpv4Endpoints(const uint16_t port)
69 {
70  using namespace boost::asio::ip;
71 
72  for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
73  for (const address_v4& addr : nic.ipv4Addresses) {
74  if (addr != ALL_V4_ENDPOINT) {
75  prohibitEndpoint(tcp::Endpoint(addr, port));
76  }
77  }
78  }
79 }
80 
81 void
82 TcpFactory::prohibitAllIpv6Endpoints(const uint16_t port)
83 {
84  using namespace boost::asio::ip;
85 
86  for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
87  for (const address_v6& addr : nic.ipv6Addresses) {
88  if (addr != ALL_V6_ENDPOINT) {
89  prohibitEndpoint(tcp::Endpoint(addr, port));
90  }
91  }
92  }
93 }
94 
95 shared_ptr<TcpChannel>
97 {
98  shared_ptr<TcpChannel> channel = findChannel(endpoint);
99  if (static_cast<bool>(channel))
100  return channel;
101 
102  channel = make_shared<TcpChannel>(endpoint);
103  m_channels[endpoint] = channel;
104  prohibitEndpoint(endpoint);
105 
106  NFD_LOG_DEBUG("Channel [" << endpoint << "] created");
107  return channel;
108 }
109 
110 shared_ptr<TcpChannel>
111 TcpFactory::createChannel(const std::string& localIp, const std::string& localPort)
112 {
113  using namespace boost::asio::ip;
114  tcp::Endpoint endpoint(address::from_string(localIp), boost::lexical_cast<uint16_t>(localPort));
115  return createChannel(endpoint);
116 }
117 
118 shared_ptr<TcpChannel>
119 TcpFactory::findChannel(const tcp::Endpoint& localEndpoint)
120 {
121  ChannelMap::iterator i = m_channels.find(localEndpoint);
122  if (i != m_channels.end())
123  return i->second;
124  else
125  return shared_ptr<TcpChannel>();
126 }
127 
128 void
130  ndn::nfd::FacePersistency persistency,
131  const FaceCreatedCallback& onCreated,
132  const FaceConnectFailedCallback& onConnectFailed)
133 {
134  if (persistency != ndn::nfd::FACE_PERSISTENCY_PERSISTENT) {
135  BOOST_THROW_EXCEPTION(Error("TcpFactory only supports persistent face"));
136  }
137 
138  BOOST_ASSERT(uri.isCanonical());
139  boost::asio::ip::address ipAddress = boost::asio::ip::address::from_string(uri.getHost());
140  tcp::Endpoint endpoint(ipAddress, boost::lexical_cast<uint16_t>(uri.getPort()));
141 
142  if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end())
143  {
144  onConnectFailed("Requested endpoint is prohibited "
145  "(reserved by this NFD or disallowed by face management protocol)");
146  return;
147  }
148 
149  // very simple logic for now
150  for (ChannelMap::iterator channel = m_channels.begin();
151  channel != m_channels.end();
152  ++channel)
153  {
154  if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
155  (channel->first.address().is_v6() && endpoint.address().is_v6()))
156  {
157  channel->second->connect(endpoint, onCreated, onConnectFailed);
158  return;
159  }
160  }
161  onConnectFailed("No channels available to connect to "
162  + boost::lexical_cast<std::string>(endpoint));
163 }
164 
165 std::list<shared_ptr<const Channel> >
167 {
168  std::list<shared_ptr<const Channel> > channels;
169  for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i)
170  {
171  channels.push_back(i->second);
172  }
173 
174  return channels;
175 }
176 
177 } // namespace nfd
function< void(const std::string &reason)> FaceConnectFailedCallback
Prototype for the callback that is called when face is failed to get created.
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:36
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
static const boost::asio::ip::address_v6 ALL_V6_ENDPOINT(boost::asio::ip::address_v6::from_string("::"))
contains information about a network interface
Exception of TcpFactory.
Definition: tcp-factory.hpp:41
TcpFactory(const std::string &defaultPort="6363")
Definition: tcp-factory.cpp:41
const std::string & getPort() const
get port
Definition: face-uri.hpp:123
std::vector< NetworkInterfaceInfo > listNetworkInterfaces()
List configured network interfaces on the system and their info.
Table::const_iterator iterator
Definition: cs-internal.hpp:41
bool isCanonical() const
determine whether this FaceUri is in canonical form
Definition: face-uri.cpp:507
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
boost::asio::ip::tcp::endpoint Endpoint
Definition: tcp-channel.hpp:35
virtual std::list< shared_ptr< const Channel > > getChannels() const
#define NFD_LOG_INIT(name)
Definition: logger.hpp:33
const std::string & getHost() const
get host (domain)
Definition: face-uri.hpp:116
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:35
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback called when face is created (as a response to incoming connection or after...
shared_ptr< TcpChannel > createChannel(const tcp::Endpoint &localEndpoint)
Create TCP-based channel using tcp::Endpoint.
Definition: tcp-factory.cpp:96
static const boost::asio::ip::address_v4 ALL_V4_ENDPOINT(boost::asio::ip::address_v4::from_string("0.0.0.0"))
virtual void createFace(const FaceUri &uri, ndn::nfd::FacePersistency persistency, const FaceCreatedCallback &onCreated, const FaceConnectFailedCallback &onConnectFailed) 1
Try to create Face using the supplied FaceUri.