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 | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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> |
Davide Pesavento | 9cec8ee | 2014-02-19 11:21:59 +0100 | [diff] [blame] | 33 | #include <sys/stat.h> // for chmod() |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 34 | |
| 35 | namespace nfd { |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 36 | namespace 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) |
| 43 | , m_acceptor(getGlobalIoService()) |
| 44 | , m_socket(getGlobalIoService()) |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 45 | , m_size(0) |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 46 | , m_wantCongestionMarking(wantCongestionMarking) |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 47 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 48 | setUri(FaceUri(m_endpoint)); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 49 | NFD_LOG_CHAN_INFO("Creating channel"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | UnixStreamChannel::~UnixStreamChannel() |
| 53 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 54 | if (isListening()) { |
| 55 | // use the non-throwing variants during destruction |
| 56 | // and ignore any errors |
| 57 | boost::system::error_code error; |
| 58 | m_acceptor.close(error); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 59 | NFD_LOG_CHAN_DEBUG("Removing socket file"); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 60 | boost::filesystem::remove(m_endpoint.path(), error); |
| 61 | } |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void |
| 65 | UnixStreamChannel::listen(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 66 | const FaceCreationFailedCallback& onAcceptFailed, |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 67 | int backlog/* = acceptor::max_connections*/) |
| 68 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 69 | if (isListening()) { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 70 | NFD_LOG_CHAN_WARN("Already listening"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 71 | return; |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 72 | } |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 73 | |
| 74 | namespace fs = boost::filesystem; |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 75 | |
| 76 | fs::path socketPath(m_endpoint.path()); |
| 77 | fs::file_type type = fs::symlink_status(socketPath).type(); |
| 78 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 79 | if (type == fs::socket_file) { |
| 80 | boost::system::error_code error; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 81 | boost::asio::local::stream_protocol::socket socket(getGlobalIoService()); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 82 | socket.connect(m_endpoint, error); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 83 | NFD_LOG_CHAN_TRACE("connect() on existing socket file returned: " << error.message()); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 84 | if (!error) { |
| 85 | // someone answered, leave the socket alone |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 86 | NDN_THROW(Error("Socket file at " + m_endpoint.path() + " belongs to another NFD process")); |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 87 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 88 | else if (error == boost::asio::error::connection_refused || |
| 89 | error == boost::asio::error::timed_out) { |
| 90 | // no one is listening on the remote side, |
| 91 | // we can safely remove the stale socket |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 92 | NFD_LOG_CHAN_DEBUG("Removing stale socket file"); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 93 | fs::remove(socketPath); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 94 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 95 | } |
| 96 | else if (type != fs::file_not_found) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 97 | NDN_THROW(Error(m_endpoint.path() + " already exists and is not a socket file")); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 98 | } |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 99 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 100 | m_acceptor.open(); |
| 101 | m_acceptor.bind(m_endpoint); |
| 102 | m_acceptor.listen(backlog); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 103 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 104 | if (::chmod(m_endpoint.path().data(), 0666) < 0) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 105 | NDN_THROW_ERRNO(Error("Failed to chmod " + m_endpoint.path())); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 106 | } |
Davide Pesavento | 9cec8ee | 2014-02-19 11:21:59 +0100 | [diff] [blame] | 107 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 108 | accept(onFaceCreated, onAcceptFailed); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 109 | NFD_LOG_CHAN_DEBUG("Started listening"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 113 | UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 114 | const FaceCreationFailedCallback& onAcceptFailed) |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 115 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 116 | m_acceptor.async_accept(m_socket, [=] (const auto& e) { this->handleAccept(e, onFaceCreated, onAcceptFailed); }); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void |
| 120 | UnixStreamChannel::handleAccept(const boost::system::error_code& error, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 121 | const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 122 | const FaceCreationFailedCallback& onAcceptFailed) |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 123 | { |
| 124 | if (error) { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 125 | if (error != boost::asio::error::operation_aborted) { |
| 126 | NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message()); |
| 127 | if (onAcceptFailed) |
| 128 | onAcceptFailed(500, "Accept failed: " + error.message()); |
| 129 | } |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 133 | NFD_LOG_CHAN_TRACE("Incoming connection via fd " << m_socket.native_handle()); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 134 | |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 135 | GenericLinkService::Options options; |
| 136 | options.allowCongestionMarking = m_wantCongestionMarking; |
| 137 | auto linkService = make_unique<GenericLinkService>(options); |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 138 | auto transport = make_unique<UnixStreamTransport>(std::move(m_socket)); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 139 | auto face = make_shared<Face>(std::move(linkService), std::move(transport)); |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 140 | |
| 141 | ++m_size; |
| 142 | connectFaceClosedSignal(*face, [this] { --m_size; }); |
| 143 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 144 | onFaceCreated(face); |
| 145 | |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 146 | // prepare accepting the next connection |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 147 | accept(onFaceCreated, onAcceptFailed); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 150 | } // namespace face |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 151 | } // namespace nfd |