blob: d99ea330c69039794bae9143cefba7328c54486b [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 Pesavento4b1921f2024-01-12 20:25:45 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Davide Pesaventob84bd3a2016-04-22 02:21:45 +02004 * 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 Pesaventob84bd3a2016-04-22 02:21:45 +020024 */
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP
27#define NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010028
Junxiao Shi61e3cc52014-03-03 20:40:28 -070029#include "channel.hpp"
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010030
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040031#include <boost/asio/local/stream_protocol.hpp>
32
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::unix_stream {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040034using Endpoint = boost::asio::local::stream_protocol::endpoint;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035} // namespace nfd::unix_stream
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010036
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037namespace nfd::face {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040038
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010039/**
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040040 * \brief Class implementing a local channel to create faces.
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010041 *
42 * Channel can create faces as a response to incoming IPC connections
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040043 * (UnixStreamChannel::listen() needs to be called for that to work).
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010044 */
Davide Pesavento3db98072021-03-09 23:03:27 -050045class UnixStreamChannel final : public Channel
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010046{
47public:
48 /**
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040049 * \brief Create a UnixStream channel for the specified \p endpoint.
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010050 *
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040051 * To enable the creation of faces upon incoming connections, one needs to
52 * explicitly call listen().
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010053 */
Eric Newberry0c841642018-01-17 15:01:00 -070054 UnixStreamChannel(const unix_stream::Endpoint& endpoint, bool wantCongestionMarking);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010055
Davide Pesavento3db98072021-03-09 23:03:27 -050056 ~UnixStreamChannel() final;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010057
Davide Pesaventoc19408d2017-04-08 00:42:55 -040058 bool
Davide Pesavento3db98072021-03-09 23:03:27 -050059 isListening() const final
Davide Pesaventoc19408d2017-04-08 00:42:55 -040060 {
Davide Pesavento4b1921f2024-01-12 20:25:45 -050061 return m_isListening;
Davide Pesaventoc19408d2017-04-08 00:42:55 -040062 }
63
64 size_t
Davide Pesavento3db98072021-03-09 23:03:27 -050065 size() const final
Davide Pesaventoc19408d2017-04-08 00:42:55 -040066 {
67 return m_size;
68 }
69
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010070 /**
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040071 * \brief Start listening.
Davide Pesavento43ff2a92017-05-18 19:50:57 -040072 *
73 * Enable listening on the Unix socket, waiting for incoming connections,
74 * and creating a face when a connection is made.
75 *
76 * Faces created in this way will have on-demand persistency.
77 *
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010078 * \param onFaceCreated Callback to notify successful creation of the face
79 * \param onAcceptFailed Callback to notify when channel fails (accept call
80 * returns an error)
81 * \param backlog The maximum length of the queue of pending incoming
82 * connections
Davide Pesavento401d1a42024-12-19 21:10:22 -050083 * \throw std::system_error
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010084 */
85 void
86 listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010087 const FaceCreationFailedCallback& onAcceptFailed,
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040088 int backlog = boost::asio::socket_base::max_listen_connections);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010089
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010090private:
Davide Pesavento6ad890a2015-03-09 03:43:17 +010091 void
92 accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010093 const FaceCreationFailedCallback& onAcceptFailed);
Davide Pesavento6ad890a2015-03-09 03:43:17 +010094
Davide Pesavento6ad890a2015-03-09 03:43:17 +010095private:
Davide Pesavento77911cc2017-04-08 22:12:30 -040096 const unix_stream::Endpoint m_endpoint;
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040097 const bool m_wantCongestionMarking;
Davide Pesavento4b1921f2024-01-12 20:25:45 -050098 bool m_isListening = false;
Davide Pesavento292e5e12015-03-13 02:08:33 +010099 boost::asio::local::stream_protocol::acceptor m_acceptor;
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400100 size_t m_size = 0;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100101};
102
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400103} // namespace nfd::face
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100104
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700105#endif // NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP