blob: ece6d0e1185598c30b39e32f9c0852831b8e0862 [file] [log] [blame]
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento6ad890a2015-03-09 03:43:17 +01003 * Copyright (c) 2014-2015, Regents of the University of California,
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 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 Pesavento6ad890a2015-03-09 03:43:17 +010027#include "unix-stream-face.hpp"
Junxiao Shi61e3cc52014-03-03 20:40:28 -070028#include "core/global-io.hpp"
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010029
30#include <boost/filesystem.hpp>
Davide Pesavento9cec8ee2014-02-19 11:21:59 +010031#include <sys/stat.h> // for chmod()
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010032
33namespace nfd {
34
35NFD_LOG_INIT("UnixStreamChannel");
36
Junxiao Shi61e3cc52014-03-03 20:40:28 -070037UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint)
Davide Pesavento292e5e12015-03-13 02:08:33 +010038 : m_endpoint(endpoint)
39 , m_acceptor(getGlobalIoService())
40 , m_socket(getGlobalIoService())
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010041{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010042 setUri(FaceUri(m_endpoint));
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010043}
44
45UnixStreamChannel::~UnixStreamChannel()
46{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010047 if (isListening()) {
48 // use the non-throwing variants during destruction
49 // and ignore any errors
50 boost::system::error_code error;
51 m_acceptor.close(error);
52 NFD_LOG_DEBUG("[" << m_endpoint << "] Removing socket file");
53 boost::filesystem::remove(m_endpoint.path(), error);
54 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010055}
56
57void
58UnixStreamChannel::listen(const FaceCreatedCallback& onFaceCreated,
59 const ConnectFailedCallback& onAcceptFailed,
60 int backlog/* = acceptor::max_connections*/)
61{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010062 if (isListening()) {
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020063 NFD_LOG_WARN("[" << m_endpoint << "] Already listening");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010064 return;
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020065 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010066
67 namespace fs = boost::filesystem;
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020068
69 fs::path socketPath(m_endpoint.path());
70 fs::file_type type = fs::symlink_status(socketPath).type();
71
Davide Pesavento6ad890a2015-03-09 03:43:17 +010072 if (type == fs::socket_file) {
73 boost::system::error_code error;
Davide Pesavento292e5e12015-03-13 02:08:33 +010074 boost::asio::local::stream_protocol::socket socket(getGlobalIoService());
Davide Pesavento6ad890a2015-03-09 03:43:17 +010075 socket.connect(m_endpoint, error);
76 NFD_LOG_TRACE("[" << m_endpoint << "] connect() on existing socket file returned: "
77 + error.message());
78 if (!error) {
79 // someone answered, leave the socket alone
80 throw Error("Socket file at " + m_endpoint.path()
81 + " belongs to another NFD process");
Davide Pesaventoe22d8c82014-04-14 04:01:52 +020082 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010083 else if (error == boost::asio::error::connection_refused ||
84 error == boost::asio::error::timed_out) {
85 // no one is listening on the remote side,
86 // we can safely remove the stale socket
87 NFD_LOG_DEBUG("[" << m_endpoint << "] Removing stale socket file");
88 fs::remove(socketPath);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010089 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010090 }
91 else if (type != fs::file_not_found) {
92 throw Error(m_endpoint.path() + " already exists and is not a socket file");
93 }
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010094
Davide Pesavento6ad890a2015-03-09 03:43:17 +010095 m_acceptor.open();
96 m_acceptor.bind(m_endpoint);
97 m_acceptor.listen(backlog);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010098
Davide Pesavento6ad890a2015-03-09 03:43:17 +010099 if (::chmod(m_endpoint.path().c_str(), 0666) < 0) {
100 throw Error("chmod(" + m_endpoint.path() + ") failed: " + std::strerror(errno));
101 }
Davide Pesavento9cec8ee2014-02-19 11:21:59 +0100102
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100103 // start accepting connections
104 accept(onFaceCreated, onAcceptFailed);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100105}
106
107void
Davide Pesavento292e5e12015-03-13 02:08:33 +0100108UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated,
109 const ConnectFailedCallback& onAcceptFailed)
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100110{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100111 m_acceptor.async_accept(m_socket, bind(&UnixStreamChannel::handleAccept, this,
112 boost::asio::placeholders::error,
113 onFaceCreated, onAcceptFailed));
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100114}
115
116void
117UnixStreamChannel::handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100118 const FaceCreatedCallback& onFaceCreated,
119 const ConnectFailedCallback& onAcceptFailed)
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100120{
121 if (error) {
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100122 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100123 return;
124
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100125 NFD_LOG_DEBUG("[" << m_endpoint << "] Accept failed: " << error.message());
126 if (onAcceptFailed)
127 onAcceptFailed(error.message());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100128 return;
129 }
130
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100131 NFD_LOG_DEBUG("[" << m_endpoint << "] Incoming connection");
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100132
Davide Pesavento292e5e12015-03-13 02:08:33 +0100133 auto remoteUri = FaceUri::fromFd(m_socket.native_handle());
134 auto localUri = FaceUri(m_socket.local_endpoint());
135 auto face = make_shared<UnixStreamFace>(remoteUri, localUri, std::move(m_socket));
136 onFaceCreated(face);
137
Davide Pesaventoe22d8c82014-04-14 04:01:52 +0200138 // prepare accepting the next connection
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100139 accept(onFaceCreated, onAcceptFailed);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100140}
141
142} // namespace nfd