blob: b60ba7886daf8af1c6202bea8c612e83dcaa9e24 [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 Pesavento19779d82019-02-14 13:40:04 -05003 * Copyright (c) 2014-2019, 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
35namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040036namespace face {
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010037
Davide Pesaventoa3148082018-04-12 18:21:54 -040038NFD_LOG_INIT(UnixStreamChannel);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010039
Eric Newberry0c841642018-01-17 15:01:00 -070040UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint,
41 bool wantCongestionMarking)
Davide Pesavento292e5e12015-03-13 02:08:33 +010042 : m_endpoint(endpoint)
43 , m_acceptor(getGlobalIoService())
44 , m_socket(getGlobalIoService())
Davide Pesaventoc19408d2017-04-08 00:42:55 -040045 , m_size(0)
Eric Newberry0c841642018-01-17 15:01:00 -070046 , m_wantCongestionMarking(wantCongestionMarking)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010047{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010048 setUri(FaceUri(m_endpoint));
Davide Pesavento77911cc2017-04-08 22:12:30 -040049 NFD_LOG_CHAN_INFO("Creating channel");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010050}
51
52UnixStreamChannel::~UnixStreamChannel()
53{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010054 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 Pesavento77911cc2017-04-08 22:12:30 -040059 NFD_LOG_CHAN_DEBUG("Removing socket file");
Davide Pesavento6ad890a2015-03-09 03:43:17 +010060 boost::filesystem::remove(m_endpoint.path(), error);
61 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010062}
63
64void
65UnixStreamChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010066 const FaceCreationFailedCallback& onAcceptFailed,
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010067 int backlog/* = acceptor::max_connections*/)
68{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010069 if (isListening()) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040070 NFD_LOG_CHAN_WARN("Already listening");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010071 return;
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020072 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010073
74 namespace fs = boost::filesystem;
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020075
76 fs::path socketPath(m_endpoint.path());
77 fs::file_type type = fs::symlink_status(socketPath).type();
78
Davide Pesavento6ad890a2015-03-09 03:43:17 +010079 if (type == fs::socket_file) {
80 boost::system::error_code error;
Davide Pesavento292e5e12015-03-13 02:08:33 +010081 boost::asio::local::stream_protocol::socket socket(getGlobalIoService());
Davide Pesavento6ad890a2015-03-09 03:43:17 +010082 socket.connect(m_endpoint, error);
Davide Pesavento77911cc2017-04-08 22:12:30 -040083 NFD_LOG_CHAN_TRACE("connect() on existing socket file returned: " << error.message());
Davide Pesavento6ad890a2015-03-09 03:43:17 +010084 if (!error) {
85 // someone answered, leave the socket alone
Davide Pesavento19779d82019-02-14 13:40:04 -050086 NDN_THROW(Error("Socket file at " + m_endpoint.path() + " belongs to another NFD process"));
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020087 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010088 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 Pesavento77911cc2017-04-08 22:12:30 -040092 NFD_LOG_CHAN_DEBUG("Removing stale socket file");
Davide Pesavento6ad890a2015-03-09 03:43:17 +010093 fs::remove(socketPath);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010094 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010095 }
96 else if (type != fs::file_not_found) {
Davide Pesavento19779d82019-02-14 13:40:04 -050097 NDN_THROW(Error(m_endpoint.path() + " already exists and is not a socket file"));
Davide Pesavento6ad890a2015-03-09 03:43:17 +010098 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010099
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100100 m_acceptor.open();
101 m_acceptor.bind(m_endpoint);
102 m_acceptor.listen(backlog);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100103
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400104 if (::chmod(m_endpoint.path().data(), 0666) < 0) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500105 NDN_THROW_ERRNO(Error("Failed to chmod " + m_endpoint.path()));
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100106 }
Davide Pesavento9cec8ee2014-02-19 11:21:59 +0100107
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100108 accept(onFaceCreated, onAcceptFailed);
Davide Pesavento77911cc2017-04-08 22:12:30 -0400109 NFD_LOG_CHAN_DEBUG("Started listening");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100110}
111
112void
Davide Pesavento292e5e12015-03-13 02:08:33 +0100113UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100114 const FaceCreationFailedCallback& onAcceptFailed)
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100115{
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400116 m_acceptor.async_accept(m_socket, [=] (const auto& e) { this->handleAccept(e, onFaceCreated, onAcceptFailed); });
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100117}
118
119void
120UnixStreamChannel::handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100121 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100122 const FaceCreationFailedCallback& onAcceptFailed)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100123{
124 if (error) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400125 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 Pesaventobc4dd8c2014-02-14 20:01:01 +0100130 return;
131 }
132
Davide Pesavento77911cc2017-04-08 22:12:30 -0400133 NFD_LOG_CHAN_TRACE("Incoming connection via fd " << m_socket.native_handle());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100134
Eric Newberry0c841642018-01-17 15:01:00 -0700135 GenericLinkService::Options options;
136 options.allowCongestionMarking = m_wantCongestionMarking;
137 auto linkService = make_unique<GenericLinkService>(options);
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400138 auto transport = make_unique<UnixStreamTransport>(std::move(m_socket));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700139 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400140
141 ++m_size;
142 connectFaceClosedSignal(*face, [this] { --m_size; });
143
Davide Pesavento292e5e12015-03-13 02:08:33 +0100144 onFaceCreated(face);
145
Davide Pesaventoe22d8c82014-04-14 04:01:52 +0200146 // prepare accepting the next connection
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100147 accept(onFaceCreated, onAcceptFailed);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100148}
149
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400150} // namespace face
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100151} // namespace nfd