2020-01-26 13:44:24 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2022-12-17 21:15:54 +00:00
|
|
|
* Copyright (c) 2022, Alexander Narsudinov <a.narsudinov@gmail.com>
|
2020-01-26 13:44:24 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-26 13:44:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/IPv4Address.h>
|
|
|
|
#include <AK/Types.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/Notifier.h>
|
2022-12-17 21:15:54 +00:00
|
|
|
#include <LibCore/System.h>
|
2020-03-14 22:41:10 +00:00
|
|
|
#include <LibCore/UDPServer.h>
|
2021-03-12 16:29:37 +00:00
|
|
|
#include <errno.h>
|
2020-01-26 13:44:24 +00:00
|
|
|
#include <stdio.h>
|
2020-08-08 11:57:03 +00:00
|
|
|
#include <unistd.h>
|
2020-01-26 13:44:24 +00:00
|
|
|
|
2020-05-23 13:31:30 +00:00
|
|
|
#ifndef SOCK_NONBLOCK
|
2021-01-13 10:45:48 +00:00
|
|
|
# include <fcntl.h>
|
2020-05-23 13:31:30 +00:00
|
|
|
# include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
|
|
|
|
2023-08-06 16:09:39 +00:00
|
|
|
UDPServer::UDPServer(EventReceiver* parent)
|
|
|
|
: EventReceiver(parent)
|
2020-01-26 13:44:24 +00:00
|
|
|
{
|
2020-05-23 13:31:30 +00:00
|
|
|
#ifdef SOCK_NONBLOCK
|
2020-01-26 13:44:24 +00:00
|
|
|
m_fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
2020-05-23 13:31:30 +00:00
|
|
|
#else
|
|
|
|
m_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
int option = 1;
|
|
|
|
ioctl(m_fd, FIONBIO, &option);
|
|
|
|
fcntl(m_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
#endif
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_fd >= 0);
|
2020-01-26 13:44:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 22:41:10 +00:00
|
|
|
UDPServer::~UDPServer()
|
2020-01-26 13:44:24 +00:00
|
|
|
{
|
2020-08-08 11:57:03 +00:00
|
|
|
::close(m_fd);
|
2020-01-26 13:44:24 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool UDPServer::bind(IPv4Address const& address, u16 port)
|
2020-01-26 13:44:24 +00:00
|
|
|
{
|
2020-04-04 09:59:33 +00:00
|
|
|
if (m_bound)
|
2020-01-26 13:44:24 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-04 09:59:33 +00:00
|
|
|
auto saddr = SocketAddress(address, port);
|
|
|
|
auto in = saddr.to_sockaddr_in();
|
2020-01-26 13:44:24 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
if (::bind(m_fd, (sockaddr const*)&in, sizeof(in)) != 0) {
|
2020-12-18 16:42:32 +00:00
|
|
|
perror("UDPServer::bind");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-10 16:56:42 +00:00
|
|
|
m_bound = true;
|
2020-09-18 07:49:51 +00:00
|
|
|
|
2023-04-23 18:59:32 +00:00
|
|
|
m_notifier = Notifier::construct(m_fd, Notifier::Type::Read, this);
|
|
|
|
m_notifier->on_activation = [this] {
|
2020-04-04 09:59:33 +00:00
|
|
|
if (on_ready_to_receive)
|
|
|
|
on_ready_to_receive();
|
2020-01-26 13:44:24 +00:00
|
|
|
};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-17 21:15:54 +00:00
|
|
|
ErrorOr<ByteBuffer> UDPServer::receive(size_t size, sockaddr_in& in)
|
2020-01-26 13:44:24 +00:00
|
|
|
{
|
2022-12-17 21:15:54 +00:00
|
|
|
auto buf = TRY(ByteBuffer::create_uninitialized(size));
|
2020-04-04 09:59:33 +00:00
|
|
|
socklen_t in_len = sizeof(in);
|
2022-12-17 21:15:54 +00:00
|
|
|
auto bytes_received = TRY(Core::System::recvfrom(m_fd, buf.data(), size, 0, (sockaddr*)&in, &in_len));
|
|
|
|
buf.resize(bytes_received);
|
2020-04-04 09:59:33 +00:00
|
|
|
return buf;
|
2020-01-26 13:44:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 22:41:10 +00:00
|
|
|
Optional<IPv4Address> UDPServer::local_address() const
|
2020-01-26 13:44:24 +00:00
|
|
|
{
|
|
|
|
if (m_fd == -1)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
sockaddr_in address;
|
|
|
|
socklen_t len = sizeof(address);
|
|
|
|
if (getsockname(m_fd, (sockaddr*)&address, &len) != 0)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return IPv4Address(address.sin_addr.s_addr);
|
|
|
|
}
|
|
|
|
|
2020-03-14 22:41:10 +00:00
|
|
|
Optional<u16> UDPServer::local_port() const
|
2020-01-26 13:44:24 +00:00
|
|
|
{
|
|
|
|
if (m_fd == -1)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
sockaddr_in address;
|
|
|
|
socklen_t len = sizeof(address);
|
|
|
|
if (getsockname(m_fd, (sockaddr*)&address, &len) != 0)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return ntohs(address.sin_port);
|
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
|
2021-09-11 20:11:30 +00:00
|
|
|
ErrorOr<size_t> UDPServer::send(ReadonlyBytes buffer, sockaddr_in const& to)
|
|
|
|
{
|
|
|
|
if (m_fd < 0) {
|
|
|
|
return Error::from_errno(EBADF);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = ::sendto(m_fd, buffer.data(), buffer.size(), 0, (sockaddr const*)&to, sizeof(to));
|
|
|
|
if (result < 0) {
|
|
|
|
return Error::from_errno(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
}
|