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 | |
| 32 | #include <boost/filesystem.hpp> |
| 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::face { |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 35 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 36 | NFD_LOG_INIT(UnixStreamChannel); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 37 | |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 38 | UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint, |
| 39 | bool wantCongestionMarking) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 40 | : m_endpoint(endpoint) |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 41 | , m_wantCongestionMarking(wantCongestionMarking) |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 42 | , m_acceptor(getGlobalIoService()) |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 43 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 44 | setUri(FaceUri(m_endpoint)); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 45 | NFD_LOG_CHAN_INFO("Creating channel"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | UnixStreamChannel::~UnixStreamChannel() |
| 49 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 50 | if (isListening()) { |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 51 | // use the non-throwing variants during destruction and ignore any errors |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 52 | boost::system::error_code ec; |
| 53 | m_acceptor.close(ec); |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 54 | NFD_LOG_CHAN_TRACE("Removing socket file"); |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 55 | boost::filesystem::remove(m_endpoint.path(), ec); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 56 | } |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void |
| 60 | UnixStreamChannel::listen(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 61 | const FaceCreationFailedCallback& onAcceptFailed, |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -0400 | [diff] [blame] | 62 | int backlog) |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 63 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 64 | if (isListening()) { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 65 | NFD_LOG_CHAN_WARN("Already listening"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 66 | return; |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 67 | } |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 68 | |
| 69 | namespace fs = boost::filesystem; |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 70 | |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 71 | fs::path socketPath = m_endpoint.path(); |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 72 | // ensure parent directory exists |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 73 | 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 Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 78 | 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 Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 97 | } |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 98 | 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 Pesavento | 9cec8ee | 2014-02-19 11:21:59 +0100 | [diff] [blame] | 122 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 123 | accept(onFaceCreated, onAcceptFailed); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 124 | NFD_LOG_CHAN_DEBUG("Started listening"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 128 | UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 129 | const FaceCreationFailedCallback& onAcceptFailed) |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 130 | { |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 131 | 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 Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 140 | } |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 141 | |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 142 | NFD_LOG_CHAN_TRACE("Incoming connection via fd " << socket.native_handle()); |
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 | 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 Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 150 | |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 151 | ++m_size; |
| 152 | connectFaceClosedSignal(*face, [this] { --m_size; }); |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 153 | |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 154 | onFaceCreated(face); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 155 | |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 156 | // prepare accepting the next connection |
| 157 | accept(onFaceCreated, onAcceptFailed); |
| 158 | }); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 161 | } // namespace nfd::face |