2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-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.
|
|
|
|
*/
|
|
|
|
|
2020-08-24 19:35:19 -06:00
|
|
|
#include <AK/Singleton.h>
|
2019-04-03 12:36:40 +02:00
|
|
|
#include <Kernel/Devices/RandomDevice.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/Net/NetworkAdapter.h>
|
2019-04-02 15:46:44 +02:00
|
|
|
#include <Kernel/Net/Routing.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/Net/UDP.h>
|
|
|
|
#include <Kernel/Net/UDPSocket.h>
|
|
|
|
#include <Kernel/Process.h>
|
2020-01-03 12:36:30 +01:00
|
|
|
#include <Kernel/Random.h>
|
2019-03-14 12:43:18 +01:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-04-18 12:50:35 +03:00
|
|
|
void UDPSocket::for_each(Function<void(const UDPSocket&)> callback)
|
2019-08-09 13:27:40 +03:00
|
|
|
{
|
2020-04-18 12:50:35 +03:00
|
|
|
LOCKER(sockets_by_port().lock(), Lock::Mode::Shared);
|
2019-08-09 13:27:40 +03:00
|
|
|
for (auto it : sockets_by_port().resource())
|
|
|
|
callback(*it.value);
|
|
|
|
}
|
|
|
|
|
2020-08-24 19:35:19 -06:00
|
|
|
static AK::Singleton<Lockable<HashMap<u16, UDPSocket*>>> s_map;
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
Lockable<HashMap<u16, UDPSocket*>>& UDPSocket::sockets_by_port()
|
2019-03-14 12:43:18 +01:00
|
|
|
{
|
|
|
|
return *s_map;
|
|
|
|
}
|
|
|
|
|
2019-08-09 09:16:12 +02:00
|
|
|
SocketHandle<UDPSocket> UDPSocket::from_port(u16 port)
|
2019-03-14 12:43:18 +01:00
|
|
|
{
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<UDPSocket> socket;
|
2019-03-14 12:43:18 +01:00
|
|
|
{
|
2020-04-18 12:50:35 +03:00
|
|
|
LOCKER(sockets_by_port().lock(), Lock::Mode::Shared);
|
2019-03-14 12:43:18 +01:00
|
|
|
auto it = sockets_by_port().resource().find(port);
|
|
|
|
if (it == sockets_by_port().resource().end())
|
2019-06-07 11:43:58 +02:00
|
|
|
return {};
|
2019-03-14 12:43:18 +01:00
|
|
|
socket = (*it).value;
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(socket);
|
2019-03-14 12:43:18 +01:00
|
|
|
}
|
2019-08-09 09:16:12 +02:00
|
|
|
return { *socket };
|
2019-03-14 12:43:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
UDPSocket::UDPSocket(int protocol)
|
|
|
|
: IPv4Socket(SOCK_DGRAM, protocol)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UDPSocket::~UDPSocket()
|
|
|
|
{
|
|
|
|
LOCKER(sockets_by_port().lock());
|
2019-05-04 16:40:34 +02:00
|
|
|
sockets_by_port().resource().remove(local_port());
|
2019-03-14 12:43:18 +01:00
|
|
|
}
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
NonnullRefPtr<UDPSocket> UDPSocket::create(int protocol)
|
2019-03-14 12:43:18 +01:00
|
|
|
{
|
|
|
|
return adopt(*new UDPSocket(protocol));
|
|
|
|
}
|
|
|
|
|
2020-12-20 16:09:48 -07:00
|
|
|
KResultOr<size_t> UDPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags)
|
2019-03-14 12:43:18 +01:00
|
|
|
{
|
2020-12-18 16:13:23 +01:00
|
|
|
auto& ipv4_packet = *(const IPv4Packet*)(raw_ipv4_packet.data());
|
2019-03-14 12:43:18 +01:00
|
|
|
auto& udp_packet = *static_cast<const UDPPacket*>(ipv4_packet.payload());
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier.
|
|
|
|
VERIFY(buffer_size >= (udp_packet.length() - sizeof(UDPPacket)));
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!buffer.write(udp_packet.payload(), udp_packet.length() - sizeof(UDPPacket)))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2019-03-14 12:43:18 +01:00
|
|
|
return udp_packet.length() - sizeof(UDPPacket);
|
|
|
|
}
|
|
|
|
|
2020-09-11 21:11:07 -06:00
|
|
|
KResultOr<size_t> UDPSocket::protocol_send(const UserOrKernelBuffer& data, size_t data_length)
|
2019-03-14 12:43:18 +01:00
|
|
|
{
|
2020-04-05 01:16:45 +04:30
|
|
|
auto routing_decision = route_to(peer_address(), local_address(), bound_interface());
|
2019-08-29 11:18:38 +10:00
|
|
|
if (routing_decision.is_zero())
|
2021-01-20 23:11:17 +01:00
|
|
|
return EHOSTUNREACH;
|
2020-12-18 17:57:58 +01:00
|
|
|
const size_t buffer_size = sizeof(UDPPacket) + data_length;
|
2021-02-12 23:45:14 +01:00
|
|
|
auto buffer = ByteBuffer::create_zeroed(buffer_size);
|
|
|
|
auto& udp_packet = *reinterpret_cast<UDPPacket*>(buffer.data());
|
2019-05-04 16:40:34 +02:00
|
|
|
udp_packet.set_source_port(local_port());
|
|
|
|
udp_packet.set_destination_port(peer_port());
|
2020-12-18 17:57:58 +01:00
|
|
|
udp_packet.set_length(buffer_size);
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!data.read(udp_packet.payload(), data_length))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2020-12-18 17:57:58 +01:00
|
|
|
|
2021-02-12 23:45:14 +01:00
|
|
|
auto result = routing_decision.adapter->send_ipv4(routing_decision.next_hop, peer_address(), IPv4Protocol::UDP, UserOrKernelBuffer::for_kernel_buffer(buffer.data()), buffer_size, ttl());
|
2021-01-31 12:13:16 +01:00
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
2019-03-14 12:43:18 +01:00
|
|
|
return data_length;
|
|
|
|
}
|
|
|
|
|
2019-08-11 16:38:20 +03:00
|
|
|
KResult UDPSocket::protocol_connect(FileDescription&, ShouldBlock)
|
|
|
|
{
|
|
|
|
m_role = Role::Connected;
|
2020-01-26 14:43:08 +01:00
|
|
|
set_connected(true);
|
2019-08-11 16:38:20 +03:00
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2019-05-04 16:40:34 +02:00
|
|
|
int UDPSocket::protocol_allocate_local_port()
|
2019-03-14 12:43:18 +01:00
|
|
|
{
|
2019-07-03 21:17:35 +02:00
|
|
|
static const u16 first_ephemeral_port = 32768;
|
|
|
|
static const u16 last_ephemeral_port = 60999;
|
|
|
|
static const u16 ephemeral_port_range_size = last_ephemeral_port - first_ephemeral_port;
|
2020-01-03 12:36:30 +01:00
|
|
|
u16 first_scan_port = first_ephemeral_port + get_good_random<u16>() % ephemeral_port_range_size;
|
2019-03-18 04:03:44 +01:00
|
|
|
|
2019-03-14 12:43:18 +01:00
|
|
|
LOCKER(sockets_by_port().lock());
|
2019-07-03 21:17:35 +02:00
|
|
|
for (u16 port = first_scan_port;;) {
|
2019-03-14 12:43:18 +01:00
|
|
|
auto it = sockets_by_port().resource().find(port);
|
|
|
|
if (it == sockets_by_port().resource().end()) {
|
2019-05-04 16:40:34 +02:00
|
|
|
set_local_port(port);
|
2019-03-14 12:43:18 +01:00
|
|
|
sockets_by_port().resource().set(port, this);
|
2019-03-18 04:03:44 +01:00
|
|
|
return port;
|
2019-03-14 12:43:18 +01:00
|
|
|
}
|
2019-03-18 04:03:44 +01:00
|
|
|
++port;
|
|
|
|
if (port > last_ephemeral_port)
|
|
|
|
port = first_ephemeral_port;
|
|
|
|
if (port == first_scan_port)
|
|
|
|
break;
|
2019-03-14 12:43:18 +01:00
|
|
|
}
|
2019-03-18 04:03:44 +01:00
|
|
|
return -EADDRINUSE;
|
2019-03-14 12:43:18 +01:00
|
|
|
}
|
2019-05-03 21:51:40 +02:00
|
|
|
|
|
|
|
KResult UDPSocket::protocol_bind()
|
|
|
|
{
|
|
|
|
LOCKER(sockets_by_port().lock());
|
2019-05-04 16:40:34 +02:00
|
|
|
if (sockets_by_port().resource().contains(local_port()))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EADDRINUSE;
|
2019-05-04 16:40:34 +02:00
|
|
|
sockets_by_port().resource().set(local_port(), this);
|
2019-05-03 21:51:40 +02:00
|
|
|
return KSuccess;
|
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|