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-2018, 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 "generic-link-service.hpp"
29 #include "core/global-io.hpp"
30 
31 #include <boost/filesystem.hpp>
32 #include <sys/stat.h> // for chmod()
33 
34 namespace nfd {
35 namespace face {
36 
38 
40  bool wantCongestionMarking)
41  : m_endpoint(endpoint)
42  , m_acceptor(getGlobalIoService())
43  , m_socket(getGlobalIoService())
44  , m_size(0)
45  , m_wantCongestionMarking(wantCongestionMarking)
46 {
47  setUri(FaceUri(m_endpoint));
48  NFD_LOG_CHAN_INFO("Creating channel");
49 }
50 
52 {
53  if (isListening()) {
54  // use the non-throwing variants during destruction
55  // and ignore any errors
56  boost::system::error_code error;
57  m_acceptor.close(error);
58  NFD_LOG_CHAN_DEBUG("Removing socket file");
59  boost::filesystem::remove(m_endpoint.path(), error);
60  }
61 }
62 
63 void
65  const FaceCreationFailedCallback& onAcceptFailed,
66  int backlog/* = acceptor::max_connections*/)
67 {
68  if (isListening()) {
69  NFD_LOG_CHAN_WARN("Already listening");
70  return;
71  }
72 
73  namespace fs = boost::filesystem;
74 
75  fs::path socketPath(m_endpoint.path());
76  fs::file_type type = fs::symlink_status(socketPath).type();
77 
78  if (type == fs::socket_file) {
79  boost::system::error_code error;
80  boost::asio::local::stream_protocol::socket socket(getGlobalIoService());
81  socket.connect(m_endpoint, error);
82  NFD_LOG_CHAN_TRACE("connect() on existing socket file returned: " << error.message());
83  if (!error) {
84  // someone answered, leave the socket alone
85  BOOST_THROW_EXCEPTION(Error("Socket file at " + m_endpoint.path()
86  + " 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  BOOST_THROW_EXCEPTION(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  BOOST_THROW_EXCEPTION(Error("chmod(" + m_endpoint.path() + ") failed: " + std::strerror(errno)));
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
void setUri(const FaceUri &uri)
Definition: channel.cpp:34
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
UnixStreamChannel-related error.
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:44
std::function< void(const shared_ptr< Face > &face)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Definition: channel.hpp:40
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
Definition: channel.cpp:40
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
Definition: channel-log.hpp:49
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
Definition: channel-log.hpp:52
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
Definition: channel-log.hpp:46
Class implementing a local channel to create faces.
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
Definition: channel-log.hpp:55
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
bool isListening() const override
Returns whether the channel is listening.
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onAcceptFailed, int backlog=boost::asio::local::stream_protocol::acceptor::max_connections)
Start listening.
UnixStreamChannel(const unix_stream::Endpoint &endpoint, bool wantCongestionMarking)
Create UnixStream channel for the specified endpoint.
boost::asio::local::stream_protocol::endpoint Endpoint
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31