Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 24 | */ |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 25 | |
| 26 | #include "unix-stream-channel.hpp" |
Davide Pesavento | cb425e8 | 2019-07-14 21:48:22 -0400 | [diff] [blame] | 27 | #include "face.hpp" |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 28 | #include "generic-link-service.hpp" |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 29 | #include "unix-stream-transport.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 30 | #include "common/global.hpp" |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 31 | |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 32 | #include <boost/filesystem/exception.hpp> |
| 33 | #include <boost/filesystem/operations.hpp> |
| 34 | #include <boost/filesystem/path.hpp> |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::face { |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 37 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 38 | NFD_LOG_INIT(UnixStreamChannel); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 39 | |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 40 | UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint, |
| 41 | bool wantCongestionMarking) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 42 | : m_endpoint(endpoint) |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 43 | , m_wantCongestionMarking(wantCongestionMarking) |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 44 | , m_acceptor(getGlobalIoService()) |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 45 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 46 | setUri(FaceUri(m_endpoint)); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 47 | NFD_LOG_CHAN_INFO("Creating channel"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | UnixStreamChannel::~UnixStreamChannel() |
| 51 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 52 | if (isListening()) { |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 53 | // use the non-throwing variants during destruction and ignore any errors |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 54 | boost::system::error_code ec; |
| 55 | m_acceptor.close(ec); |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 56 | NFD_LOG_CHAN_TRACE("Removing socket file"); |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 57 | boost::filesystem::remove(m_endpoint.path(), ec); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 58 | } |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void |
| 62 | UnixStreamChannel::listen(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 63 | const FaceCreationFailedCallback& onAcceptFailed, |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -0400 | [diff] [blame] | 64 | int backlog) |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 65 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 66 | if (isListening()) { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 67 | NFD_LOG_CHAN_WARN("Already listening"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 68 | return; |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 69 | } |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 70 | |
| 71 | namespace fs = boost::filesystem; |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 72 | |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 73 | fs::path socketPath = m_endpoint.path(); |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 74 | // ensure parent directory exists |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 75 | 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 Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 80 | 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 Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 99 | } |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 100 | 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 Pesavento | 9cec8ee | 2014-02-19 11:21:59 +0100 | [diff] [blame] | 124 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 125 | accept(onFaceCreated, onAcceptFailed); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 126 | NFD_LOG_CHAN_DEBUG("Started listening"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 130 | UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 131 | const FaceCreationFailedCallback& onAcceptFailed) |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 132 | { |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 133 | 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 Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 142 | } |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 143 | |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 144 | NFD_LOG_CHAN_TRACE("Incoming connection via fd " << socket.native_handle()); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 145 | |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 146 | 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 Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 152 | |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 153 | ++m_size; |
| 154 | connectFaceClosedSignal(*face, [this] { --m_size; }); |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 155 | |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 156 | onFaceCreated(face); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 157 | |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 158 | // prepare accepting the next connection |
| 159 | accept(onFaceCreated, onAcceptFailed); |
| 160 | }); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 163 | } // namespace nfd::face |