blob: 2b09ed437697eedc5b741e7ac6ded99a3a3918ce [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
Davide Pesavento152874a2024-02-20 22:07:07 -050032#include <boost/filesystem/exception.hpp>
33#include <boost/filesystem/operations.hpp>
34#include <boost/filesystem/path.hpp>
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010035
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::face {
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010037
Davide Pesaventoa3148082018-04-12 18:21:54 -040038NFD_LOG_INIT(UnixStreamChannel);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010039
Eric Newberry0c841642018-01-17 15:01:00 -070040UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint,
41 bool wantCongestionMarking)
Davide Pesavento292e5e12015-03-13 02:08:33 +010042 : m_endpoint(endpoint)
Eric Newberry0c841642018-01-17 15:01:00 -070043 , m_wantCongestionMarking(wantCongestionMarking)
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040044 , m_acceptor(getGlobalIoService())
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010045{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010046 setUri(FaceUri(m_endpoint));
Davide Pesavento77911cc2017-04-08 22:12:30 -040047 NFD_LOG_CHAN_INFO("Creating channel");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010048}
49
50UnixStreamChannel::~UnixStreamChannel()
51{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010052 if (isListening()) {
Davide Pesavento4c957712024-01-01 15:40:06 -050053 // use the non-throwing variants during destruction and ignore any errors
Davide Pesavento4b1921f2024-01-12 20:25:45 -050054 boost::system::error_code ec;
55 m_acceptor.close(ec);
Davide Pesavento4c957712024-01-01 15:40:06 -050056 NFD_LOG_CHAN_TRACE("Removing socket file");
Davide Pesavento4b1921f2024-01-12 20:25:45 -050057 boost::filesystem::remove(m_endpoint.path(), ec);
Davide Pesavento6ad890a2015-03-09 03:43:17 +010058 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010059}
60
61void
62UnixStreamChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010063 const FaceCreationFailedCallback& onAcceptFailed,
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040064 int backlog)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010065{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010066 if (isListening()) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040067 NFD_LOG_CHAN_WARN("Already listening");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010068 return;
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020069 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010070
71 namespace fs = boost::filesystem;
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020072
Davide Pesavento4c957712024-01-01 15:40:06 -050073 fs::path socketPath = m_endpoint.path();
Davide Pesavento4b1921f2024-01-12 20:25:45 -050074 // ensure parent directory exists
Davide Pesavento4c957712024-01-01 15:40:06 -050075 fs::path parent = socketPath.parent_path();
76 if (!parent.empty() && fs::create_directories(parent)) {
77 NFD_LOG_CHAN_TRACE("Created directory " << parent);
78 }
79
Davide Pesavento4b1921f2024-01-12 20:25:45 -050080 boost::system::error_code ec;
81 fs::file_type type = fs::symlink_status(socketPath).type();
82 if (type == fs::socket_file) {
83 // if the socket file already exists, there may be another instance
84 // of NFD running on the system: make sure we don't steal its socket
85 boost::asio::local::stream_protocol::socket socket(getGlobalIoService());
86 socket.connect(m_endpoint, ec);
87 NFD_LOG_CHAN_TRACE("connect() on existing socket file returned: " << ec.message());
88 if (!ec) {
89 // someone answered, leave the socket alone
90 ec = boost::system::errc::make_error_code(boost::system::errc::address_in_use);
91 NDN_THROW_NO_STACK(fs::filesystem_error("UnixStreamChannel::listen", socketPath, ec));
92 }
93 else if (ec == boost::asio::error::connection_refused ||
94 ec == boost::asio::error::timed_out) {
95 // no one is listening on the remote side, we can safely remove the stale socket
96 NFD_LOG_CHAN_DEBUG("Removing stale socket file");
97 fs::remove(socketPath);
98 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010099 }
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500100 else if (type != fs::file_not_found) {
101 // the file exists but is not a socket: this is a fatal error as we cannot
102 // safely overwrite the file without potentially risking data loss
103 ec = boost::system::errc::make_error_code(boost::system::errc::not_a_socket);
104 NDN_THROW_NO_STACK(fs::filesystem_error("UnixStreamChannel::listen", socketPath, ec));
105 }
106
107 try {
108 m_acceptor.open();
109 m_acceptor.bind(m_endpoint);
110 m_acceptor.listen(backlog);
111 }
112 catch (const boost::system::system_error& e) {
113 // exceptions thrown by Boost.Asio are very terse, add more context
114 NDN_THROW_NO_STACK(fs::filesystem_error("UnixStreamChannel::listen: "s + e.std::runtime_error::what(),
115 socketPath, e.code()));
116 }
117
118 // do this here so that, even if the calls below fail,
119 // the destructor will still remove the socket file
120 m_isListening = true;
121
122 fs::permissions(socketPath, fs::owner_read | fs::group_read | fs::others_read |
123 fs::owner_write | fs::group_write | fs::others_write);
Davide Pesavento9cec8ee2014-02-19 11:21:59 +0100124
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100125 accept(onFaceCreated, onAcceptFailed);
Davide Pesavento77911cc2017-04-08 22:12:30 -0400126 NFD_LOG_CHAN_DEBUG("Started listening");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100127}
128
129void
Davide Pesavento292e5e12015-03-13 02:08:33 +0100130UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100131 const FaceCreationFailedCallback& onAcceptFailed)
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100132{
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400133 m_acceptor.async_accept([=] (const boost::system::error_code& error,
134 boost::asio::local::stream_protocol::socket socket) {
135 if (error) {
136 if (error != boost::asio::error::operation_aborted) {
137 NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
138 if (onAcceptFailed)
139 onAcceptFailed(500, "Accept failed: " + error.message());
140 }
141 return;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400142 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100143
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400144 NFD_LOG_CHAN_TRACE("Incoming connection via fd " << socket.native_handle());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100145
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400146 GenericLinkService::Options options;
147 options.allowCongestionMarking = m_wantCongestionMarking;
148 auto linkService = make_unique<GenericLinkService>(options);
149 auto transport = make_unique<UnixStreamTransport>(std::move(socket));
150 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
151 face->setChannel(weak_from_this());
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400152
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400153 ++m_size;
154 connectFaceClosedSignal(*face, [this] { --m_size; });
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400155
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400156 onFaceCreated(face);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100157
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400158 // prepare accepting the next connection
159 accept(onFaceCreated, onAcceptFailed);
160 });
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100161}
162
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400163} // namespace nfd::face