/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2014-2024,  Regents of the University of California,
 *                           Arizona Board of Regents,
 *                           Colorado State University,
 *                           University Pierre & Marie Curie, Sorbonne University,
 *                           Washington University in St. Louis,
 *                           Beijing Institute of Technology,
 *                           The University of Memphis.
 *
 * This file is part of NFD (Named Data Networking Forwarding Daemon).
 * See AUTHORS.md for complete list of NFD authors and contributors.
 *
 * NFD is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "unix-stream-channel.hpp"
#include "face.hpp"
#include "generic-link-service.hpp"
#include "unix-stream-transport.hpp"
#include "common/global.hpp"

#include <boost/filesystem/exception.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>

namespace nfd::face {

NFD_LOG_INIT(UnixStreamChannel);

UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint,
                                     bool wantCongestionMarking)
  : m_endpoint(endpoint)
  , m_wantCongestionMarking(wantCongestionMarking)
  , m_acceptor(getGlobalIoService())
{
  setUri(FaceUri(m_endpoint));
  NFD_LOG_CHAN_INFO("Creating channel");
}

UnixStreamChannel::~UnixStreamChannel()
{
  if (isListening()) {
    // use the non-throwing variants during destruction and ignore any errors
    boost::system::error_code ec;
    m_acceptor.close(ec);
    NFD_LOG_CHAN_TRACE("Removing socket file");
    boost::filesystem::remove(m_endpoint.path(), ec);
  }
}

void
UnixStreamChannel::listen(const FaceCreatedCallback& onFaceCreated,
                          const FaceCreationFailedCallback& onAcceptFailed,
                          int backlog)
{
  if (isListening()) {
    NFD_LOG_CHAN_WARN("Already listening");
    return;
  }

  namespace fs = boost::filesystem;

  fs::path socketPath = m_endpoint.path();
  // ensure parent directory exists
  fs::path parent = socketPath.parent_path();
  if (!parent.empty() && fs::create_directories(parent)) {
    NFD_LOG_CHAN_TRACE("Created directory " << parent);
  }

  boost::system::error_code ec;
  fs::file_type type = fs::symlink_status(socketPath).type();
  if (type == fs::socket_file) {
    // if the socket file already exists, there may be another instance
    // of NFD running on the system: make sure we don't steal its socket
    boost::asio::local::stream_protocol::socket socket(getGlobalIoService());
    socket.connect(m_endpoint, ec);
    NFD_LOG_CHAN_TRACE("connect() on existing socket file returned: " << ec.message());
    if (!ec) {
      // someone answered, leave the socket alone
      ec = boost::system::errc::make_error_code(boost::system::errc::address_in_use);
      NDN_THROW_NO_STACK(fs::filesystem_error("UnixStreamChannel::listen", socketPath, ec));
    }
    else if (ec == boost::asio::error::connection_refused ||
             ec == boost::asio::error::timed_out) {
      // no one is listening on the remote side, we can safely remove the stale socket
      NFD_LOG_CHAN_DEBUG("Removing stale socket file");
      fs::remove(socketPath);
    }
  }
  else if (type != fs::file_not_found) {
    // the file exists but is not a socket: this is a fatal error as we cannot
    // safely overwrite the file without potentially risking data loss
    ec = boost::system::errc::make_error_code(boost::system::errc::not_a_socket);
    NDN_THROW_NO_STACK(fs::filesystem_error("UnixStreamChannel::listen", socketPath, ec));
  }

  try {
    m_acceptor.open();
    m_acceptor.bind(m_endpoint);
    m_acceptor.listen(backlog);
  }
  catch (const boost::system::system_error& e) {
    // exceptions thrown by Boost.Asio are very terse, add more context
    NDN_THROW_NO_STACK(fs::filesystem_error("UnixStreamChannel::listen: "s + e.std::runtime_error::what(),
                                            socketPath, e.code()));
  }

  // do this here so that, even if the calls below fail,
  // the destructor will still remove the socket file
  m_isListening = true;

  fs::permissions(socketPath, fs::owner_read | fs::group_read | fs::others_read |
                              fs::owner_write | fs::group_write | fs::others_write);

  accept(onFaceCreated, onAcceptFailed);
  NFD_LOG_CHAN_DEBUG("Started listening");
}

void
UnixStreamChannel::accept(const FaceCreatedCallback& onFaceCreated,
                          const FaceCreationFailedCallback& onAcceptFailed)
{
  m_acceptor.async_accept([=] (const boost::system::error_code& error,
                               boost::asio::local::stream_protocol::socket socket) {
    if (error) {
      if (error != boost::asio::error::operation_aborted) {
        NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
        if (onAcceptFailed)
          onAcceptFailed(500, "Accept failed: " + error.message());
      }
      return;
    }

    NFD_LOG_CHAN_TRACE("Incoming connection via fd " << socket.native_handle());

    GenericLinkService::Options options;
    options.allowCongestionMarking = m_wantCongestionMarking;
    auto linkService = make_unique<GenericLinkService>(options);
    auto transport = make_unique<UnixStreamTransport>(std::move(socket));
    auto face = make_shared<Face>(std::move(linkService), std::move(transport));
    face->setChannel(weak_from_this());

    ++m_size;
    connectFaceClosedSignal(*face, [this] { --m_size; });

    onFaceCreated(face);

    // prepare accepting the next connection
    accept(onFaceCreated, onAcceptFailed);
  });
}

} // namespace nfd::face
