ftpiiu_plugin/source/socket.cpp

338 lines
7.4 KiB
C++
Raw Normal View History

2020-04-05 21:16:16 +02:00
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program 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.
//
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
#include "socket.h"
#include "log.h"
#include <fcntl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <poll.h>
#include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstring>
///////////////////////////////////////////////////////////////////////////
Socket::~Socket ()
{
if (m_listening)
2020-04-10 04:21:25 +02:00
info ("Stop listening on [%s]:%u\n", m_sockName.name (), m_sockName.port ());
2020-04-06 07:36:03 +02:00
2020-04-05 21:16:16 +02:00
if (m_connected)
2020-04-10 04:21:25 +02:00
info ("Closing connection to [%s]:%u\n", m_peerName.name (), m_peerName.port ());
2020-04-06 07:36:03 +02:00
2020-04-05 21:16:16 +02:00
if (::close (m_fd) != 0)
2020-04-10 04:21:25 +02:00
error ("close: %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
}
Socket::Socket (int const fd_) : m_fd (fd_), m_listening (false), m_connected (false)
{
}
Socket::Socket (int const fd_, SockAddr const &sockName_, SockAddr const &peerName_)
: m_sockName (sockName_),
m_peerName (peerName_),
m_fd (fd_),
m_listening (false),
m_connected (true)
{
}
UniqueSocket Socket::accept ()
{
SockAddr addr;
socklen_t addrLen = sizeof (struct sockaddr_storage);
auto const fd = ::accept (m_fd, addr, &addrLen);
if (fd < 0)
{
2020-04-10 04:21:25 +02:00
error ("accept: %s\n", std::strerror (errno));
2020-04-06 07:36:03 +02:00
return nullptr;
2020-04-05 21:16:16 +02:00
}
2020-04-10 04:21:25 +02:00
info ("Accepted connection from [%s]:%u\n", addr.name (), addr.port ());
2020-04-05 21:16:16 +02:00
return UniqueSocket (new Socket (fd, m_sockName, addr));
}
int Socket::atMark ()
{
auto const rc = ::sockatmark (m_fd);
if (rc < 0)
2020-04-10 04:21:25 +02:00
error ("sockatmark: %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
return rc;
}
bool Socket::bind (SockAddr const &addr_)
{
switch (static_cast<struct sockaddr_storage const &> (addr_).ss_family)
{
case AF_INET:
if (::bind (m_fd, addr_, sizeof (struct sockaddr_in)) != 0)
{
2020-04-10 04:21:25 +02:00
error ("bind: %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
return false;
}
break;
#ifndef _3DS
case AF_INET6:
if (::bind (m_fd, addr_, sizeof (struct sockaddr_in6)) != 0)
{
2020-04-10 04:21:25 +02:00
error ("bind: %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
return false;
}
break;
#endif
default:
errno = EINVAL;
2020-04-10 04:21:25 +02:00
error ("bind: %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
break;
}
if (addr_.port () == 0)
{
2020-04-06 07:36:03 +02:00
// get socket name due to request for ephemeral port
2020-04-05 21:16:16 +02:00
socklen_t addrLen = sizeof (struct sockaddr_storage);
if (::getsockname (m_fd, m_sockName, &addrLen) != 0)
2020-04-10 04:21:25 +02:00
error ("getsockname: %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
}
else
m_sockName = addr_;
return true;
}
bool Socket::connect (SockAddr const &addr_)
{
if (::connect (m_fd, addr_, sizeof (struct sockaddr_storage)) != 0)
{
if (errno != EINPROGRESS)
2020-04-10 04:21:25 +02:00
error ("connect: %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
else
{
m_peerName = addr_;
m_connected = true;
2020-04-10 04:21:25 +02:00
info ("Connecting to [%s]:%u\n", addr_.name (), addr_.port ());
2020-04-05 21:16:16 +02:00
}
return false;
}
m_peerName = addr_;
m_connected = true;
2020-04-10 04:21:25 +02:00
info ("Connected to [%s]:%u\n", addr_.name (), addr_.port ());
2020-04-05 21:16:16 +02:00
return true;
}
bool Socket::listen (int const backlog_)
{
if (::listen (m_fd, backlog_) != 0)
{
2020-04-10 04:21:25 +02:00
error ("listen: %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
return false;
}
m_listening = true;
return true;
}
bool Socket::shutdown (int const how_)
{
if (::shutdown (m_fd, how_) != 0)
{
2020-04-10 04:21:25 +02:00
info ("shutdown: %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
return false;
}
return true;
}
bool Socket::setLinger (bool const enable_, std::chrono::seconds const time_)
{
struct linger linger;
linger.l_onoff = enable_;
linger.l_linger = time_.count ();
if (::setsockopt (m_fd, SOL_SOCKET, SO_LINGER, &linger, sizeof (linger)) != 0)
{
2020-04-10 04:21:25 +02:00
error ("setsockopt(SO_LINGER, %s, %lus): %s\n",
2020-04-05 21:16:16 +02:00
enable_ ? "on" : "off",
static_cast<unsigned long> (time_.count ()),
std::strerror (errno));
return false;
}
return true;
}
bool Socket::setNonBlocking (bool const nonBlocking_)
{
auto flags = ::fcntl (m_fd, F_GETFL, 0);
if (flags == -1)
{
2020-04-10 04:21:25 +02:00
error ("fcntl(F_GETFL): %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
return false;
}
if (nonBlocking_)
flags |= O_NONBLOCK;
else
flags &= ~O_NONBLOCK;
if (::fcntl (m_fd, F_SETFL, flags) != 0)
{
2020-04-10 04:21:25 +02:00
error ("fcntl(F_SETFL, %d): %s\n", flags, std::strerror (errno));
2020-04-05 21:16:16 +02:00
return false;
}
return true;
}
bool Socket::setReuseAddress (bool const reuse_)
{
int reuse = reuse_;
if (::setsockopt (m_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse)) != 0)
{
2020-04-10 04:21:25 +02:00
error ("setsockopt(SO_REUSEADDR, %s): %s\n", reuse_ ? "yes" : "no", std::strerror (errno));
2020-04-05 21:16:16 +02:00
return false;
}
return true;
}
bool Socket::setRecvBufferSize (std::size_t const size_)
{
int size = size_;
if (::setsockopt (m_fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) != 0)
{
2020-04-10 04:21:25 +02:00
error ("setsockopt(SO_RCVBUF, %zu): %s\n", size_, std::strerror (errno));
2020-04-05 21:16:16 +02:00
return false;
}
return true;
}
bool Socket::setSendBufferSize (std::size_t const size_)
{
int size = size_;
if (::setsockopt (m_fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) != 0)
{
2020-04-10 04:21:25 +02:00
error ("setsockopt(SO_SNDBUF, %zu): %s\n", size_, std::strerror (errno));
2020-04-05 21:16:16 +02:00
return false;
}
return true;
}
ssize_t Socket::read (void *const buffer_, std::size_t const size_, bool const oob_)
{
assert (buffer_);
assert (size_);
auto const rc = ::recv (m_fd, buffer_, size_, oob_ ? MSG_OOB : 0);
if (rc < 0 && errno != EWOULDBLOCK)
2020-04-10 04:21:25 +02:00
error ("recv: %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
return rc;
}
ssize_t Socket::read (IOBuffer &buffer_, bool const oob_)
{
auto const rc = read (buffer_.freeArea (), buffer_.freeSize (), oob_);
if (rc > 0)
buffer_.markUsed (rc);
return rc;
}
ssize_t Socket::write (void const *const buffer_, std::size_t const size_)
{
assert (buffer_);
assert (size_);
auto const rc = ::send (m_fd, buffer_, size_, 0);
if (rc < 0 && errno != EWOULDBLOCK)
2020-04-10 04:21:25 +02:00
error ("send: %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
return rc;
}
ssize_t Socket::write (IOBuffer &buffer_)
{
auto const rc = write (buffer_.usedArea (), buffer_.usedSize ());
if (rc > 0)
buffer_.markFree (rc);
return rc;
}
SockAddr const &Socket::sockName () const
{
return m_sockName;
}
SockAddr const &Socket::peerName () const
{
return m_peerName;
}
UniqueSocket Socket::create ()
{
auto const fd = ::socket (AF_INET, SOCK_STREAM, 0);
if (fd < 0)
{
2020-04-10 04:21:25 +02:00
error ("socket: %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
return nullptr;
}
return UniqueSocket (new Socket (fd));
}
int Socket::poll (PollInfo *const info_,
std::size_t const count_,
std::chrono::milliseconds const timeout_)
{
if (count_ == 0)
return 0;
auto const pfd = std::make_unique<struct pollfd[]> (count_);
for (std::size_t i = 0; i < count_; ++i)
{
pfd[i].fd = info_[i].socket.get ().m_fd;
pfd[i].events = info_[i].events;
pfd[i].revents = 0;
}
auto const rc = ::poll (pfd.get (), count_, timeout_.count ());
if (rc < 0)
{
2020-04-10 04:21:25 +02:00
error ("poll: %s\n", std::strerror (errno));
2020-04-05 21:16:16 +02:00
return rc;
}
for (std::size_t i = 0; i < count_; ++i)
info_[i].revents = pfd[i].revents;
return rc;
}