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-2020, 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;
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
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) {
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  face->setChannel(shared_from_this()); // use weak_from_this() in C++17
141 
142  ++m_size;
143  connectFaceClosedSignal(*face, [this] { --m_size; });
144 
145  onFaceCreated(face);
146 
147  // prepare accepting the next connection
148  accept(onFaceCreated, onAcceptFailed);
149 }
150 
151 } // namespace face
152 } // namespace nfd
void setUri(const FaceUri &uri)
Definition: channel.cpp:35
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
Accept any value the remote endpoint offers.
Definition: enabled.hpp:207
bool isListening() const final
Returns whether the channel is listening.
detail::SimulatorIo & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:49
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:90
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
Definition: channel.cpp:47
#define NDN_THROW(e)
Definition: exception.hpp:61
#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:39
#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
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.
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:86
boost::asio::local::stream_protocol::endpoint Endpoint
Catch-all error for socket component errors that don&#39;t fit in other categories.
Definition: base.hpp:83
#define NDN_THROW_ERRNO(e)
Definition: exception.hpp:68