blob: 906d23e9117deb9f9c6b092b86d1161f9d10c0dc [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 Pesaventod91fe6d2023-10-04 21:40:02 -04003 * Copyright (c) 2014-2023, 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>
Davide Pesavento9cec8ee2014-02-19 11:21:59 +010033#include <sys/stat.h> // for chmod()
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010034
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::face {
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010036
Davide Pesaventoa3148082018-04-12 18:21:54 -040037NFD_LOG_INIT(UnixStreamChannel);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010038
Eric Newberry0c841642018-01-17 15:01:00 -070039UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint,
40 bool wantCongestionMarking)
Davide Pesavento292e5e12015-03-13 02:08:33 +010041 : m_endpoint(endpoint)
Eric Newberry0c841642018-01-17 15:01:00 -070042 , m_wantCongestionMarking(wantCongestionMarking)
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040043 , m_acceptor(getGlobalIoService())
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010044{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010045 setUri(FaceUri(m_endpoint));
Davide Pesavento77911cc2017-04-08 22:12:30 -040046 NFD_LOG_CHAN_INFO("Creating channel");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010047}
48
49UnixStreamChannel::~UnixStreamChannel()
50{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010051 if (isListening()) {
52 // use the non-throwing variants during destruction
53 // and ignore any errors
54 boost::system::error_code error;
55 m_acceptor.close(error);
Davide Pesavento77911cc2017-04-08 22:12:30 -040056 NFD_LOG_CHAN_DEBUG("Removing socket file");
Davide Pesavento6ad890a2015-03-09 03:43:17 +010057 boost::filesystem::remove(m_endpoint.path(), error);
58 }
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
73 fs::path socketPath(m_endpoint.path());
74 fs::file_type type = fs::symlink_status(socketPath).type();
75
Davide Pesavento6ad890a2015-03-09 03:43:17 +010076 if (type == fs::socket_file) {
77 boost::system::error_code error;
Davide Pesavento292e5e12015-03-13 02:08:33 +010078 boost::asio::local::stream_protocol::socket socket(getGlobalIoService());
Davide Pesavento6ad890a2015-03-09 03:43:17 +010079 socket.connect(m_endpoint, error);
Davide Pesavento77911cc2017-04-08 22:12:30 -040080 NFD_LOG_CHAN_TRACE("connect() on existing socket file returned: " << error.message());
Davide Pesavento6ad890a2015-03-09 03:43:17 +010081 if (!error) {
82 // someone answered, leave the socket alone
Davide Pesavento19779d82019-02-14 13:40:04 -050083 NDN_THROW(Error("Socket file at " + m_endpoint.path() + " belongs to another NFD process"));
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020084 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010085 else if (error == boost::asio::error::connection_refused ||
86 error == boost::asio::error::timed_out) {
87 // no one is listening on the remote side,
88 // we can safely remove the stale socket
Davide Pesavento77911cc2017-04-08 22:12:30 -040089 NFD_LOG_CHAN_DEBUG("Removing stale socket file");
Davide Pesavento6ad890a2015-03-09 03:43:17 +010090 fs::remove(socketPath);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010091 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010092 }
93 else if (type != fs::file_not_found) {
Davide Pesavento19779d82019-02-14 13:40:04 -050094 NDN_THROW(Error(m_endpoint.path() + " already exists and is not a socket file"));
Davide Pesavento6ad890a2015-03-09 03:43:17 +010095 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010096
Davide Pesavento6ad890a2015-03-09 03:43:17 +010097 m_acceptor.open();
98 m_acceptor.bind(m_endpoint);
99 m_acceptor.listen(backlog);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100100
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400101 if (::chmod(m_endpoint.path().data(), 0666) < 0) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500102 NDN_THROW_ERRNO(Error("Failed to chmod " + m_endpoint.path()));
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100103 }
Davide Pesavento9cec8ee2014-02-19 11:21:59 +0100104
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100105 accept(onFaceCreated, onAcceptFailed);
Davide Pesavento77911cc2017-04-08 22:12:30 -0400106 NFD_LOG_CHAN_DEBUG("Started listening");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100107}
108
109void
Davide Pesavento292e5e12015-03-13 02:08:33 +0100110UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100111 const FaceCreationFailedCallback& onAcceptFailed)
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100112{
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400113 m_acceptor.async_accept([=] (const boost::system::error_code& error,
114 boost::asio::local::stream_protocol::socket socket) {
115 if (error) {
116 if (error != boost::asio::error::operation_aborted) {
117 NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
118 if (onAcceptFailed)
119 onAcceptFailed(500, "Accept failed: " + error.message());
120 }
121 return;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400122 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100123
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400124 NFD_LOG_CHAN_TRACE("Incoming connection via fd " << socket.native_handle());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100125
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400126 GenericLinkService::Options options;
127 options.allowCongestionMarking = m_wantCongestionMarking;
128 auto linkService = make_unique<GenericLinkService>(options);
129 auto transport = make_unique<UnixStreamTransport>(std::move(socket));
130 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
131 face->setChannel(weak_from_this());
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400132
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400133 ++m_size;
134 connectFaceClosedSignal(*face, [this] { --m_size; });
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400135
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400136 onFaceCreated(face);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100137
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400138 // prepare accepting the next connection
139 accept(onFaceCreated, onAcceptFailed);
140 });
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100141}
142
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400143} // namespace nfd::face