blob: b253a708ce95bd93a5ab6de3fd7d95eeb1f613de [file] [log] [blame]
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento8fd15e62017-04-06 19:58:54 -04003 * Copyright (c) 2014-2017, 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"
Yukai Tu74c895d2015-09-21 01:11:51 -070027#include "generic-link-service.hpp"
Yukai Tu74c895d2015-09-21 01:11:51 -070028#include "unix-stream-transport.hpp"
Junxiao Shi61e3cc52014-03-03 20:40:28 -070029#include "core/global-io.hpp"
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010030
31#include <boost/filesystem.hpp>
Davide Pesavento9cec8ee2014-02-19 11:21:59 +010032#include <sys/stat.h> // for chmod()
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010033
34namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040035namespace face {
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010036
37NFD_LOG_INIT("UnixStreamChannel");
38
Junxiao Shi61e3cc52014-03-03 20:40:28 -070039UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint)
Davide Pesavento292e5e12015-03-13 02:08:33 +010040 : m_endpoint(endpoint)
41 , m_acceptor(getGlobalIoService())
42 , m_socket(getGlobalIoService())
Davide Pesaventoc19408d2017-04-08 00:42:55 -040043 , m_size(0)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010044{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010045 setUri(FaceUri(m_endpoint));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010046}
47
48UnixStreamChannel::~UnixStreamChannel()
49{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010050 if (isListening()) {
51 // use the non-throwing variants during destruction
52 // and ignore any errors
53 boost::system::error_code error;
54 m_acceptor.close(error);
55 NFD_LOG_DEBUG("[" << m_endpoint << "] Removing socket file");
56 boost::filesystem::remove(m_endpoint.path(), error);
57 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010058}
59
60void
61UnixStreamChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010062 const FaceCreationFailedCallback& onAcceptFailed,
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010063 int backlog/* = acceptor::max_connections*/)
64{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010065 if (isListening()) {
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020066 NFD_LOG_WARN("[" << m_endpoint << "] Already listening");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010067 return;
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020068 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010069
70 namespace fs = boost::filesystem;
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020071
72 fs::path socketPath(m_endpoint.path());
73 fs::file_type type = fs::symlink_status(socketPath).type();
74
Davide Pesavento6ad890a2015-03-09 03:43:17 +010075 if (type == fs::socket_file) {
76 boost::system::error_code error;
Davide Pesavento292e5e12015-03-13 02:08:33 +010077 boost::asio::local::stream_protocol::socket socket(getGlobalIoService());
Davide Pesavento6ad890a2015-03-09 03:43:17 +010078 socket.connect(m_endpoint, error);
79 NFD_LOG_TRACE("[" << m_endpoint << "] connect() on existing socket file returned: "
80 + error.message());
81 if (!error) {
82 // someone answered, leave the socket alone
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070083 BOOST_THROW_EXCEPTION(Error("Socket file at " + m_endpoint.path()
84 + " belongs to another NFD process"));
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020085 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010086 else if (error == boost::asio::error::connection_refused ||
87 error == boost::asio::error::timed_out) {
88 // no one is listening on the remote side,
89 // we can safely remove the stale socket
90 NFD_LOG_DEBUG("[" << m_endpoint << "] Removing stale socket file");
91 fs::remove(socketPath);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010092 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010093 }
94 else if (type != fs::file_not_found) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070095 BOOST_THROW_EXCEPTION(Error(m_endpoint.path() + " already exists and is not a socket file"));
Davide Pesavento6ad890a2015-03-09 03:43:17 +010096 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010097
Davide Pesavento6ad890a2015-03-09 03:43:17 +010098 m_acceptor.open();
99 m_acceptor.bind(m_endpoint);
100 m_acceptor.listen(backlog);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100101
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100102 if (::chmod(m_endpoint.path().c_str(), 0666) < 0) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700103 BOOST_THROW_EXCEPTION(Error("chmod(" + m_endpoint.path() + ") failed: " +
104 std::strerror(errno)));
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100105 }
Davide Pesavento9cec8ee2014-02-19 11:21:59 +0100106
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100107 // start accepting connections
108 accept(onFaceCreated, onAcceptFailed);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100109}
110
111void
Davide Pesavento292e5e12015-03-13 02:08:33 +0100112UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100113 const FaceCreationFailedCallback& onAcceptFailed)
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100114{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100115 m_acceptor.async_accept(m_socket, bind(&UnixStreamChannel::handleAccept, this,
116 boost::asio::placeholders::error,
117 onFaceCreated, onAcceptFailed));
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100118}
119
120void
121UnixStreamChannel::handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100122 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100123 const FaceCreationFailedCallback& onAcceptFailed)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100124{
125 if (error) {
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100126 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100127 return;
128
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100129 NFD_LOG_DEBUG("[" << m_endpoint << "] Accept failed: " << error.message());
130 if (onAcceptFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700131 onAcceptFailed(500, "Accept failed: " + error.message());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100132 return;
133 }
134
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100135 NFD_LOG_DEBUG("[" << m_endpoint << "] Incoming connection");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100136
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400137 auto linkService = make_unique<GenericLinkService>();
138 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