NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
unix-stream-channel.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include "unix-stream-channel.hpp"
27 #include "face.hpp"
28 #include "generic-link-service.hpp"
30 #include "common/global.hpp"
31 
32 #include <boost/filesystem.hpp>
33 #include <sys/stat.h> // for chmod()
34 
35 namespace nfd {
36 namespace face {
37 
39 
41  bool wantCongestionMarking)
42  : m_endpoint(endpoint)
43  , m_acceptor(getGlobalIoService())
44  , m_socket(getGlobalIoService())
45  , m_size(0)
46  , m_wantCongestionMarking(wantCongestionMarking)
47 {
48  setUri(FaceUri(m_endpoint));
49  NFD_LOG_CHAN_INFO("Creating channel");
50 }
51 
53 {
54  if (isListening()) {
55  // use the non-throwing variants during destruction
56  // and ignore any errors
57  boost::system::error_code error;
58  m_acceptor.close(error);
59  NFD_LOG_CHAN_DEBUG("Removing socket file");
60  boost::filesystem::remove(m_endpoint.path(), error);
61  }
62 }
63 
64 void
66  const FaceCreationFailedCallback& onAcceptFailed,
67  int backlog/* = acceptor::max_connections*/)
68 {
69  if (isListening()) {
70  NFD_LOG_CHAN_WARN("Already listening");
71  return;
72  }
73 
74  namespace fs = boost::filesystem;
75 
76  fs::path socketPath(m_endpoint.path());
77  fs::file_type type = fs::symlink_status(socketPath).type();
78 
79  if (type == fs::socket_file) {
80  boost::system::error_code error;
81  boost::asio::local::stream_protocol::socket socket(getGlobalIoService());
82  socket.connect(m_endpoint, error);
83  NFD_LOG_CHAN_TRACE("connect() on existing socket file returned: " << error.message());
84  if (!error) {
85  // someone answered, leave the socket alone
86  NDN_THROW(Error("Socket file at " + m_endpoint.path() + " belongs to another NFD process"));
87  }
88  else if (error == boost::asio::error::connection_refused ||
89  error == boost::asio::error::timed_out) {
90  // no one is listening on the remote side,
91  // we can safely remove the stale socket
92  NFD_LOG_CHAN_DEBUG("Removing stale socket file");
93  fs::remove(socketPath);
94  }
95  }
96  else if (type != fs::file_not_found) {
97  NDN_THROW(Error(m_endpoint.path() + " already exists and is not a socket file"));
98  }
99 
100  m_acceptor.open();
101  m_acceptor.bind(m_endpoint);
102  m_acceptor.listen(backlog);
103 
104  if (::chmod(m_endpoint.path().data(), 0666) < 0) {
105  NDN_THROW_ERRNO(Error("Failed to chmod " + m_endpoint.path()));
106  }
107 
108  accept(onFaceCreated, onAcceptFailed);
109  NFD_LOG_CHAN_DEBUG("Started listening");
110 }
111 
112 void
113 UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated,
114  const FaceCreationFailedCallback& onAcceptFailed)
115 {
116  m_acceptor.async_accept(m_socket, [=] (const auto& e) { this->handleAccept(e, onFaceCreated, onAcceptFailed); });
117 }
118 
119 void
120 UnixStreamChannel::handleAccept(const boost::system::error_code& error,
121  const FaceCreatedCallback& onFaceCreated,
122  const FaceCreationFailedCallback& onAcceptFailed)
123 {
124  if (error) {
125  if (error != boost::asio::error::operation_aborted) {
126  NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
127  if (onAcceptFailed)
128  onAcceptFailed(500, "Accept failed: " + error.message());
129  }
130  return;
131  }
132 
133  NFD_LOG_CHAN_TRACE("Incoming connection via fd " << m_socket.native_handle());
134 
135  GenericLinkService::Options options;
136  options.allowCongestionMarking = m_wantCongestionMarking;
137  auto linkService = make_unique<GenericLinkService>(options);
138  auto transport = make_unique<UnixStreamTransport>(std::move(m_socket));
139  auto face = make_shared<Face>(std::move(linkService), std::move(transport));
140 
141  ++m_size;
142  connectFaceClosedSignal(*face, [this] { --m_size; });
143 
144  onFaceCreated(face);
145 
146  // prepare accepting the next connection
147  accept(onFaceCreated, onAcceptFailed);
148 }
149 
150 } // namespace face
151 } // namespace nfd
nfd::face::UnixStreamChannel::isListening
bool isListening() const override
Returns whether the channel is listening.
Definition: unix-stream-channel.hpp:72
global.hpp
NDN_THROW_ERRNO
#define NDN_THROW_ERRNO(e)
Definition: exception.hpp:68
nfd::face::UnixStreamChannel::UnixStreamChannel
UnixStreamChannel(const unix_stream::Endpoint &endpoint, bool wantCongestionMarking)
Create UnixStream channel for the specified endpoint.
Definition: unix-stream-channel.cpp:40
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::FaceUri
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:45
nfd::face::UnixStreamChannel::~UnixStreamChannel
~UnixStreamChannel() override
Definition: unix-stream-channel.cpp:52
NFD_LOG_CHAN_DEBUG
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
Definition: channel-log.hpp:49
nfd::getGlobalIoService
detail::SimulatorIo & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:49
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
NFD_LOG_CHAN_INFO
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
Definition: channel-log.hpp:52
NFD_LOG_CHAN_WARN
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
Definition: channel-log.hpp:55
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
NFD_LOG_CHAN_TRACE
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
Definition: channel-log.hpp:46
nfd::face::UnixStreamChannel::listen
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onAcceptFailed, int backlog=boost::asio::local::stream_protocol::acceptor::max_connections)
Start listening.
Definition: unix-stream-channel.cpp:65
nfd::face::connectFaceClosedSignal
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
Definition: channel.cpp:41
unix-stream-transport.hpp
unix-stream-channel.hpp
nfd::face::FaceCreatedCallback
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...
Definition: channel.hpp:74
face.hpp
nfd::unix_stream::Endpoint
boost::asio::local::stream_protocol::endpoint Endpoint
Definition: unix-stream-channel.hpp:34
nfd::face::Channel::setUri
void setUri(const FaceUri &uri)
Definition: channel.cpp:35
nfd::face::UnixStreamChannel::Error
UnixStreamChannel-related error.
Definition: unix-stream-channel.hpp:52
nfd::face::FaceCreationFailedCallback
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.
Definition: channel.hpp:78
nfd::face::UnixStreamChannel
Class implementing a local channel to create faces.
Definition: unix-stream-channel.hpp:46
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31