Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 3 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 24 | */ |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 25 | |
| 26 | #include "unix-stream-channel.hpp" |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 27 | #include "unix-stream-face.hpp" |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 28 | #include "core/global-io.hpp" |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 29 | |
| 30 | #include <boost/filesystem.hpp> |
Davide Pesavento | 9cec8ee | 2014-02-19 11:21:59 +0100 | [diff] [blame] | 31 | #include <sys/stat.h> // for chmod() |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 32 | |
| 33 | namespace nfd { |
| 34 | |
| 35 | NFD_LOG_INIT("UnixStreamChannel"); |
| 36 | |
| 37 | using namespace boost::asio::local; |
| 38 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 39 | UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint) |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 40 | : m_acceptor(getGlobalIoService()) |
| 41 | , m_endpoint(endpoint) |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 42 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 43 | setUri(FaceUri(m_endpoint)); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | UnixStreamChannel::~UnixStreamChannel() |
| 47 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 48 | if (isListening()) { |
| 49 | // use the non-throwing variants during destruction |
| 50 | // and ignore any errors |
| 51 | boost::system::error_code error; |
| 52 | m_acceptor.close(error); |
| 53 | NFD_LOG_DEBUG("[" << m_endpoint << "] Removing socket file"); |
| 54 | boost::filesystem::remove(m_endpoint.path(), error); |
| 55 | } |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void |
| 59 | UnixStreamChannel::listen(const FaceCreatedCallback& onFaceCreated, |
| 60 | const ConnectFailedCallback& onAcceptFailed, |
| 61 | int backlog/* = acceptor::max_connections*/) |
| 62 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 63 | if (isListening()) { |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 64 | NFD_LOG_WARN("[" << m_endpoint << "] Already listening"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 65 | return; |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 66 | } |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 67 | |
| 68 | namespace fs = boost::filesystem; |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 69 | |
| 70 | fs::path socketPath(m_endpoint.path()); |
| 71 | fs::file_type type = fs::symlink_status(socketPath).type(); |
| 72 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 73 | if (type == fs::socket_file) { |
| 74 | boost::system::error_code error; |
| 75 | stream_protocol::socket socket(getGlobalIoService()); |
| 76 | socket.connect(m_endpoint, error); |
| 77 | NFD_LOG_TRACE("[" << m_endpoint << "] connect() on existing socket file returned: " |
| 78 | + error.message()); |
| 79 | if (!error) { |
| 80 | // someone answered, leave the socket alone |
| 81 | throw Error("Socket file at " + m_endpoint.path() |
| 82 | + " belongs to another NFD process"); |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 83 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 84 | else if (error == boost::asio::error::connection_refused || |
| 85 | error == boost::asio::error::timed_out) { |
| 86 | // no one is listening on the remote side, |
| 87 | // we can safely remove the stale socket |
| 88 | NFD_LOG_DEBUG("[" << m_endpoint << "] Removing stale socket file"); |
| 89 | fs::remove(socketPath); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 90 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 91 | } |
| 92 | else if (type != fs::file_not_found) { |
| 93 | throw Error(m_endpoint.path() + " already exists and is not a socket file"); |
| 94 | } |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 95 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 96 | m_acceptor.open(); |
| 97 | m_acceptor.bind(m_endpoint); |
| 98 | m_acceptor.listen(backlog); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 99 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 100 | if (::chmod(m_endpoint.path().c_str(), 0666) < 0) { |
| 101 | throw Error("chmod(" + m_endpoint.path() + ") failed: " + std::strerror(errno)); |
| 102 | } |
Davide Pesavento | 9cec8ee | 2014-02-19 11:21:59 +0100 | [diff] [blame] | 103 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 104 | // start accepting connections |
| 105 | accept(onFaceCreated, onAcceptFailed); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 109 | UnixStreamChannel::accept(const FaceCreatedCallback &onFaceCreated, |
| 110 | const ConnectFailedCallback &onAcceptFailed) |
| 111 | { |
| 112 | auto socket = make_shared<stream_protocol::socket>(ref(getGlobalIoService())); |
| 113 | |
| 114 | m_acceptor.async_accept(*socket, |
| 115 | bind(&UnixStreamChannel::handleAccept, this, |
| 116 | boost::asio::placeholders::error, |
| 117 | socket, onFaceCreated, onAcceptFailed)); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | UnixStreamChannel::handleAccept(const boost::system::error_code& error, |
| 122 | const shared_ptr<stream_protocol::socket>& socket, |
| 123 | const FaceCreatedCallback& onFaceCreated, |
| 124 | const ConnectFailedCallback& onAcceptFailed) |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 125 | { |
| 126 | if (error) { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 127 | if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 128 | return; |
| 129 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 130 | NFD_LOG_DEBUG("[" << m_endpoint << "] Accept failed: " << error.message()); |
| 131 | if (onAcceptFailed) |
| 132 | onAcceptFailed(error.message()); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 136 | NFD_LOG_DEBUG("[" << m_endpoint << "] Incoming connection"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 137 | |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 138 | // prepare accepting the next connection |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 139 | accept(onFaceCreated, onAcceptFailed); |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 140 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 141 | shared_ptr<UnixStreamFace> face = make_shared<UnixStreamFace>(socket); |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 142 | onFaceCreated(face); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } // namespace nfd |