2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2021-12-25 14:59:40 +00:00
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-05 10:47:30 +00:00
|
|
|
#include <AK/IPv4Address.h>
|
|
|
|
#include <AK/Types.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/Notifier.h>
|
2023-02-08 22:05:44 +00:00
|
|
|
#include <LibCore/Socket.h>
|
2021-12-25 14:59:40 +00:00
|
|
|
#include <LibCore/System.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/TCPServer.h>
|
2019-08-05 10:47:30 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
|
|
|
|
2023-08-06 16:09:39 +00:00
|
|
|
ErrorOr<NonnullRefPtr<TCPServer>> TCPServer::try_create(EventReceiver* parent)
|
2019-08-05 10:47:30 +00:00
|
|
|
{
|
2020-05-23 13:31:30 +00:00
|
|
|
#ifdef SOCK_NONBLOCK
|
2021-12-25 14:59:40 +00:00
|
|
|
int fd = TRY(Core::System::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
|
2020-05-23 13:31:30 +00:00
|
|
|
#else
|
2021-12-25 14:59:40 +00:00
|
|
|
int fd = TRY(Core::System::socket(AF_INET, SOCK_STREAM, 0));
|
2020-05-23 13:31:30 +00:00
|
|
|
int option = 1;
|
2021-12-25 14:59:40 +00:00
|
|
|
TRY(Core::System::ioctl(fd, FIONBIO, &option));
|
|
|
|
TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC));
|
2020-05-23 13:31:30 +00:00
|
|
|
#endif
|
2021-12-25 14:59:40 +00:00
|
|
|
|
|
|
|
return adopt_nonnull_ref_or_enomem(new (nothrow) TCPServer(fd, parent));
|
|
|
|
}
|
|
|
|
|
2023-08-06 16:09:39 +00:00
|
|
|
TCPServer::TCPServer(int fd, EventReceiver* parent)
|
|
|
|
: EventReceiver(parent)
|
2021-12-25 14:59:40 +00:00
|
|
|
, m_fd(fd)
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_fd >= 0);
|
2019-08-05 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
TCPServer::~TCPServer()
|
2019-08-05 10:47:30 +00:00
|
|
|
{
|
2021-12-25 14:59:40 +00:00
|
|
|
MUST(Core::System::close(m_fd));
|
2019-08-05 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 13:28:43 +00:00
|
|
|
ErrorOr<void> TCPServer::listen(IPv4Address const& address, u16 port, AllowAddressReuse allow_address_reuse)
|
2019-08-05 10:47:30 +00:00
|
|
|
{
|
|
|
|
if (m_listening)
|
2021-12-25 14:59:40 +00:00
|
|
|
return Error::from_errno(EADDRINUSE);
|
2019-08-05 10:47:30 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
auto socket_address = SocketAddress(address, port);
|
2019-08-05 10:47:30 +00:00
|
|
|
auto in = socket_address.to_sockaddr_in();
|
2022-11-14 13:28:43 +00:00
|
|
|
|
|
|
|
if (allow_address_reuse == AllowAddressReuse::Yes) {
|
|
|
|
int option = 1;
|
|
|
|
TRY(Core::System::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)));
|
|
|
|
}
|
|
|
|
|
2021-12-25 14:59:40 +00:00
|
|
|
TRY(Core::System::bind(m_fd, (sockaddr const*)&in, sizeof(in)));
|
|
|
|
TRY(Core::System::listen(m_fd, 5));
|
2019-08-05 10:47:30 +00:00
|
|
|
m_listening = true;
|
|
|
|
|
2023-04-23 18:59:32 +00:00
|
|
|
m_notifier = Notifier::construct(m_fd, Notifier::Type::Read, this);
|
|
|
|
m_notifier->on_activation = [this] {
|
2019-08-05 10:47:30 +00:00
|
|
|
if (on_ready_to_accept)
|
|
|
|
on_ready_to_accept();
|
|
|
|
};
|
2021-12-25 14:59:40 +00:00
|
|
|
return {};
|
2019-08-05 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 14:59:40 +00:00
|
|
|
ErrorOr<void> TCPServer::set_blocking(bool blocking)
|
2021-05-25 19:40:56 +00:00
|
|
|
{
|
2021-12-25 14:59:40 +00:00
|
|
|
int flags = TRY(Core::System::fcntl(m_fd, F_GETFL, 0));
|
2021-05-25 19:40:56 +00:00
|
|
|
if (blocking)
|
2021-12-25 14:59:40 +00:00
|
|
|
TRY(Core::System::fcntl(m_fd, F_SETFL, flags & ~O_NONBLOCK));
|
2021-05-25 19:40:56 +00:00
|
|
|
else
|
2021-12-25 14:59:40 +00:00
|
|
|
TRY(Core::System::fcntl(m_fd, F_SETFL, flags | O_NONBLOCK));
|
|
|
|
return {};
|
2021-05-25 19:40:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 22:05:44 +00:00
|
|
|
ErrorOr<NonnullOwnPtr<TCPSocket>> TCPServer::accept()
|
2019-08-05 10:47:30 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_listening);
|
2019-08-05 10:47:30 +00:00
|
|
|
sockaddr_in in;
|
|
|
|
socklen_t in_size = sizeof(in);
|
2024-02-27 13:32:29 +00:00
|
|
|
#if !defined(AK_OS_MACOS) && !defined(AK_OS_IOS) && !defined(AK_OS_HAIKU)
|
2021-12-25 14:59:40 +00:00
|
|
|
int accepted_fd = TRY(Core::System::accept4(m_fd, (sockaddr*)&in, &in_size, SOCK_NONBLOCK | SOCK_CLOEXEC));
|
2021-05-16 10:13:19 +00:00
|
|
|
#else
|
2021-12-25 14:59:40 +00:00
|
|
|
int accepted_fd = TRY(Core::System::accept(m_fd, (sockaddr*)&in, &in_size));
|
2021-05-16 10:13:19 +00:00
|
|
|
#endif
|
|
|
|
|
2023-02-08 22:05:44 +00:00
|
|
|
auto socket = TRY(TCPSocket::adopt_fd(accepted_fd));
|
2021-09-12 11:55:40 +00:00
|
|
|
|
2024-02-27 13:32:29 +00:00
|
|
|
#if defined(AK_OS_MACOS) || defined(AK_OS_IOS) || defined(AK_OS_HAIKU)
|
2021-09-12 11:55:40 +00:00
|
|
|
// FIXME: Ideally, we should let the caller decide whether it wants the
|
|
|
|
// socket to be nonblocking or not, but there are currently places
|
|
|
|
// which depend on this.
|
2021-12-29 22:31:45 +00:00
|
|
|
TRY(socket->set_blocking(false));
|
|
|
|
TRY(socket->set_close_on_exec(true));
|
2021-05-16 10:13:19 +00:00
|
|
|
#endif
|
|
|
|
|
2021-09-12 11:55:40 +00:00
|
|
|
return socket;
|
2019-08-05 10:47:30 +00:00
|
|
|
}
|
2019-10-07 08:07:24 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Optional<IPv4Address> TCPServer::local_address() const
|
2019-10-07 08:07:24 +00:00
|
|
|
{
|
|
|
|
if (m_fd == -1)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
sockaddr_in address;
|
|
|
|
socklen_t len = sizeof(address);
|
|
|
|
if (getsockname(m_fd, (sockaddr*)&address, &len) != 0)
|
2019-11-04 12:07:41 +00:00
|
|
|
return {};
|
2019-10-07 08:07:24 +00:00
|
|
|
|
|
|
|
return IPv4Address(address.sin_addr.s_addr);
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Optional<u16> TCPServer::local_port() const
|
2019-10-07 08:07:24 +00:00
|
|
|
{
|
|
|
|
if (m_fd == -1)
|
2019-11-04 12:07:41 +00:00
|
|
|
return {};
|
2019-10-07 08:07:24 +00:00
|
|
|
|
|
|
|
sockaddr_in address;
|
|
|
|
socklen_t len = sizeof(address);
|
|
|
|
if (getsockname(m_fd, (sockaddr*)&address, &len) != 0)
|
2019-11-04 12:07:41 +00:00
|
|
|
return {};
|
2019-10-07 08:07:24 +00:00
|
|
|
|
|
|
|
return ntohs(address.sin_port);
|
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
|
|
|
|
}
|