blob: a1abe9c43ea49210304fd81f5c0f02b18205f489 [file] [log] [blame]
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry0c841642018-01-17 15:01:00 -07002/*
Davide Pesavento4c957712024-01-01 15:40:06 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Davide Pesavento6ad890a2015-03-09 03:43:17 +01004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Davide Pesavento6ad890a2015-03-09 03:43:17 +010024 */
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010025
26#include "unix-stream-channel.hpp"
Davide Pesaventocb425e82019-07-14 21:48:22 -040027#include "face.hpp"
Yukai Tu74c895d2015-09-21 01:11:51 -070028#include "generic-link-service.hpp"
Yukai Tu74c895d2015-09-21 01:11:51 -070029#include "unix-stream-transport.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040030#include "common/global.hpp"
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010031
32#include <boost/filesystem.hpp>
33
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034namespace nfd::face {
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010035
Davide Pesaventoa3148082018-04-12 18:21:54 -040036NFD_LOG_INIT(UnixStreamChannel);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010037
Eric Newberry0c841642018-01-17 15:01:00 -070038UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint,
39 bool wantCongestionMarking)
Davide Pesavento292e5e12015-03-13 02:08:33 +010040 : m_endpoint(endpoint)
Eric Newberry0c841642018-01-17 15:01:00 -070041 , m_wantCongestionMarking(wantCongestionMarking)
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040042 , m_acceptor(getGlobalIoService())
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010043{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010044 setUri(FaceUri(m_endpoint));
Davide Pesavento77911cc2017-04-08 22:12:30 -040045 NFD_LOG_CHAN_INFO("Creating channel");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010046}
47
48UnixStreamChannel::~UnixStreamChannel()
49{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010050 if (isListening()) {
Davide Pesavento4c957712024-01-01 15:40:06 -050051 // use the non-throwing variants during destruction and ignore any errors
Davide Pesavento4b1921f2024-01-12 20:25:45 -050052 boost::system::error_code ec;
53 m_acceptor.close(ec);
Davide Pesavento4c957712024-01-01 15:40:06 -050054 NFD_LOG_CHAN_TRACE("Removing socket file");
Davide Pesavento4b1921f2024-01-12 20:25:45 -050055 boost::filesystem::remove(m_endpoint.path(), ec);
Davide Pesavento6ad890a2015-03-09 03:43:17 +010056 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010057}
58
59void
60UnixStreamChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010061 const FaceCreationFailedCallback& onAcceptFailed,
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040062 int backlog)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010063{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010064 if (isListening()) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040065 NFD_LOG_CHAN_WARN("Already listening");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010066 return;
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020067 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010068
69 namespace fs = boost::filesystem;
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020070
Davide Pesavento4c957712024-01-01 15:40:06 -050071 fs::path socketPath = m_endpoint.path();
Davide Pesavento4b1921f2024-01-12 20:25:45 -050072 // ensure parent directory exists
Davide Pesavento4c957712024-01-01 15:40:06 -050073 fs::path parent = socketPath.parent_path();
74 if (!parent.empty() && fs::create_directories(parent)) {
75 NFD_LOG_CHAN_TRACE("Created directory " << parent);
76 }
77
Davide Pesavento4b1921f2024-01-12 20:25:45 -050078 boost::system::error_code ec;
79 fs::file_type type = fs::symlink_status(socketPath).type();
80 if (type == fs::socket_file) {
81 // if the socket file already exists, there may be another instance
82 // of NFD running on the system: make sure we don't steal its socket
83 boost::asio::local::stream_protocol::socket socket(getGlobalIoService());
84 socket.connect(m_endpoint, ec);
85 NFD_LOG_CHAN_TRACE("connect() on existing socket file returned: " << ec.message());
86 if (!ec) {
87 // someone answered, leave the socket alone
88 ec = boost::system::errc::make_error_code(boost::system::errc::address_in_use);
89 NDN_THROW_NO_STACK(fs::filesystem_error("UnixStreamChannel::listen", socketPath, ec));
90 }
91 else if (ec == boost::asio::error::connection_refused ||
92 ec == boost::asio::error::timed_out) {
93 // no one is listening on the remote side, we can safely remove the stale socket
94 NFD_LOG_CHAN_DEBUG("Removing stale socket file");
95 fs::remove(socketPath);
96 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010097 }
Davide Pesavento4b1921f2024-01-12 20:25:45 -050098 else if (type != fs::file_not_found) {
99 // the file exists but is not a socket: this is a fatal error as we cannot
100 // safely overwrite the file without potentially risking data loss
101 ec = boost::system::errc::make_error_code(boost::system::errc::not_a_socket);
102 NDN_THROW_NO_STACK(fs::filesystem_error("UnixStreamChannel::listen", socketPath, ec));
103 }
104
105 try {
106 m_acceptor.open();
107 m_acceptor.bind(m_endpoint);
108 m_acceptor.listen(backlog);
109 }
110 catch (const boost::system::system_error& e) {
111 // exceptions thrown by Boost.Asio are very terse, add more context
112 NDN_THROW_NO_STACK(fs::filesystem_error("UnixStreamChannel::listen: "s + e.std::runtime_error::what(),
113 socketPath, e.code()));
114 }
115
116 // do this here so that, even if the calls below fail,
117 // the destructor will still remove the socket file
118 m_isListening = true;
119
120 fs::permissions(socketPath, fs::owner_read | fs::group_read | fs::others_read |
121 fs::owner_write | fs::group_write | fs::others_write);
Davide Pesavento9cec8ee2014-02-19 11:21:59 +0100122
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100123 accept(onFaceCreated, onAcceptFailed);
Davide Pesavento77911cc2017-04-08 22:12:30 -0400124 NFD_LOG_CHAN_DEBUG("Started listening");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100125}
126
127void
Davide Pesavento292e5e12015-03-13 02:08:33 +0100128UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100129 const FaceCreationFailedCallback& onAcceptFailed)
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100130{
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400131 m_acceptor.async_accept([=] (const boost::system::error_code& error,
132 boost::asio::local::stream_protocol::socket socket) {
133 if (error) {
134 if (error != boost::asio::error::operation_aborted) {
135 NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
136 if (onAcceptFailed)
137 onAcceptFailed(500, "Accept failed: " + error.message());
138 }
139 return;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400140 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100141
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400142 NFD_LOG_CHAN_TRACE("Incoming connection via fd " << socket.native_handle());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100143
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400144 GenericLinkService::Options options;
145 options.allowCongestionMarking = m_wantCongestionMarking;
146 auto linkService = make_unique<GenericLinkService>(options);
147 auto transport = make_unique<UnixStreamTransport>(std::move(socket));
148 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
149 face->setChannel(weak_from_this());
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400150
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400151 ++m_size;
152 connectFaceClosedSignal(*face, [this] { --m_size; });
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400153
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400154 onFaceCreated(face);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100155
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400156 // prepare accepting the next connection
157 accept(onFaceCreated, onAcceptFailed);
158 });
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100159}
160
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400161} // namespace nfd::face