2020-01-26 13:44:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/IPv4Address.h>
|
|
|
|
#include <AK/Types.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/Notifier.h>
|
2020-03-14 22:41:10 +00:00
|
|
|
#include <LibCore/UDPServer.h>
|
|
|
|
#include <LibCore/UDPSocket.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
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
|
|
|
|
2020-03-14 22:41:10 +00:00
|
|
|
UDPServer::UDPServer(Object* parent)
|
2020-02-02 11:34:39 +00:00
|
|
|
: Object(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
|
2020-01-26 13:44:24 +00:00
|
|
|
ASSERT(m_fd >= 0);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-04 09:59:33 +00:00
|
|
|
bool UDPServer::bind(const IPv4Address& 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
|
|
|
|
2020-12-18 16:42:32 +00:00
|
|
|
if (::bind(m_fd, (const sockaddr*)&in, sizeof(in)) != 0) {
|
|
|
|
perror("UDPServer::bind");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-10 16:56:42 +00:00
|
|
|
m_bound = true;
|
2020-09-18 07:49:51 +00:00
|
|
|
|
2020-02-10 13:15:56 +00:00
|
|
|
m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
|
2020-01-26 13:44:24 +00:00
|
|
|
m_notifier->on_ready_to_read = [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;
|
|
|
|
}
|
|
|
|
|
2020-04-04 09:59:33 +00:00
|
|
|
ByteBuffer UDPServer::receive(size_t size, sockaddr_in& in)
|
2020-01-26 13:44:24 +00:00
|
|
|
{
|
2020-08-08 13:02:28 +00:00
|
|
|
auto buf = ByteBuffer::create_uninitialized(size);
|
2020-04-04 09:59:33 +00:00
|
|
|
socklen_t in_len = sizeof(in);
|
|
|
|
ssize_t rlen = ::recvfrom(m_fd, buf.data(), size, 0, (sockaddr*)&in, &in_len);
|
|
|
|
if (rlen < 0) {
|
2021-01-11 12:04:22 +00:00
|
|
|
dbgln("recvfrom: {}", strerror(errno));
|
2020-04-04 09:59:33 +00:00
|
|
|
return {};
|
2020-01-26 13:44:24 +00:00
|
|
|
}
|
2020-08-08 13:02:28 +00:00
|
|
|
|
|
|
|
buf.trim(rlen);
|
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
|
|
|
|
|
|
|
}
|