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-2022, 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::face {
36 
37 NFD_LOG_INIT(UnixStreamChannel);
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  NDN_THROW(Error("Socket file at " + m_endpoint.path() + " belongs to another NFD process"));
86  }
87  else if (error == boost::asio::error::connection_refused ||
88  error == boost::asio::error::timed_out) {
89  // no one is listening on the remote side,
90  // we can safely remove the stale socket
91  NFD_LOG_CHAN_DEBUG("Removing stale socket file");
92  fs::remove(socketPath);
93  }
94  }
95  else if (type != fs::file_not_found) {
96  NDN_THROW(Error(m_endpoint.path() + " already exists and is not a socket file"));
97  }
98 
99  m_acceptor.open();
100  m_acceptor.bind(m_endpoint);
101  m_acceptor.listen(backlog);
102 
103  if (::chmod(m_endpoint.path().data(), 0666) < 0) {
104  NDN_THROW_ERRNO(Error("Failed to chmod " + m_endpoint.path()));
105  }
106 
107  accept(onFaceCreated, onAcceptFailed);
108  NFD_LOG_CHAN_DEBUG("Started listening");
109 }
110 
111 void
112 UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated,
113  const FaceCreationFailedCallback& onAcceptFailed)
114 {
115  m_acceptor.async_accept(m_socket, [=] (const auto& e) { this->handleAccept(e, onFaceCreated, onAcceptFailed); });
116 }
117 
118 void
119 UnixStreamChannel::handleAccept(const boost::system::error_code& error,
120  const FaceCreatedCallback& onFaceCreated,
121  const FaceCreationFailedCallback& onAcceptFailed)
122 {
123  if (error) {
124  if (error != boost::asio::error::operation_aborted) {
125  NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
126  if (onAcceptFailed)
127  onAcceptFailed(500, "Accept failed: " + error.message());
128  }
129  return;
130  }
131 
132  NFD_LOG_CHAN_TRACE("Incoming connection via fd " << m_socket.native_handle());
133 
134  GenericLinkService::Options options;
135  options.allowCongestionMarking = m_wantCongestionMarking;
136  auto linkService = make_unique<GenericLinkService>(options);
137  auto transport = make_unique<UnixStreamTransport>(std::move(m_socket));
138  auto face = make_shared<Face>(std::move(linkService), std::move(transport));
139  face->setChannel(weak_from_this());
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 nfd::face
void setUri(const FaceUri &uri) noexcept
Definition: channel.cpp:34
UnixStreamChannel-related error.
bool isListening() const final
Returns whether the channel is listening.
UnixStreamChannel(const unix_stream::Endpoint &endpoint, bool wantCongestionMarking)
Create UnixStream channel for the specified endpoint.
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onAcceptFailed, int backlog=boost::asio::local::stream_protocol::acceptor::max_connections)
Start listening.
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
Definition: channel-log.hpp:49
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
Definition: channel-log.hpp:52
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
Definition: channel-log.hpp:55
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
Definition: channel-log.hpp:46
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
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:92
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:88
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
Definition: channel.cpp:46
boost::asio::local::stream_protocol::endpoint Endpoint
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:36