2019-04-03 13:36:40 +03:00
|
|
|
#include <Kernel/Devices/RandomDevice.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Net/NetworkAdapter.h>
|
2019-04-02 16:46:44 +03:00
|
|
|
#include <Kernel/Net/Routing.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Net/UDP.h>
|
|
|
|
#include <Kernel/Net/UDPSocket.h>
|
|
|
|
#include <Kernel/Process.h>
|
2019-03-14 14:43:18 +03:00
|
|
|
|
2019-08-09 13:27:40 +03:00
|
|
|
void UDPSocket::for_each(Function<void(UDPSocket&)> callback)
|
|
|
|
{
|
|
|
|
LOCKER(sockets_by_port().lock());
|
|
|
|
for (auto it : sockets_by_port().resource())
|
|
|
|
callback(*it.value);
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
Lockable<HashMap<u16, UDPSocket*>>& UDPSocket::sockets_by_port()
|
2019-03-14 14:43:18 +03:00
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
static Lockable<HashMap<u16, UDPSocket*>>* s_map;
|
2019-03-14 14:43:18 +03:00
|
|
|
if (!s_map)
|
2019-07-03 22:17:35 +03:00
|
|
|
s_map = new Lockable<HashMap<u16, UDPSocket*>>;
|
2019-03-14 14:43:18 +03:00
|
|
|
return *s_map;
|
|
|
|
}
|
|
|
|
|
2019-08-09 10:16:12 +03:00
|
|
|
SocketHandle<UDPSocket> UDPSocket::from_port(u16 port)
|
2019-03-14 14:43:18 +03:00
|
|
|
{
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<UDPSocket> socket;
|
2019-03-14 14:43:18 +03:00
|
|
|
{
|
|
|
|
LOCKER(sockets_by_port().lock());
|
|
|
|
auto it = sockets_by_port().resource().find(port);
|
|
|
|
if (it == sockets_by_port().resource().end())
|
2019-06-07 12:43:58 +03:00
|
|
|
return {};
|
2019-03-14 14:43:18 +03:00
|
|
|
socket = (*it).value;
|
|
|
|
ASSERT(socket);
|
|
|
|
}
|
2019-08-09 10:16:12 +03:00
|
|
|
return { *socket };
|
2019-03-14 14:43:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
UDPSocket::UDPSocket(int protocol)
|
|
|
|
: IPv4Socket(SOCK_DGRAM, protocol)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UDPSocket::~UDPSocket()
|
|
|
|
{
|
|
|
|
LOCKER(sockets_by_port().lock());
|
2019-05-04 17:40:34 +03:00
|
|
|
sockets_by_port().resource().remove(local_port());
|
2019-03-14 14:43:18 +03:00
|
|
|
}
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
NonnullRefPtr<UDPSocket> UDPSocket::create(int protocol)
|
2019-03-14 14:43:18 +03:00
|
|
|
{
|
|
|
|
return adopt(*new UDPSocket(protocol));
|
|
|
|
}
|
|
|
|
|
2019-08-04 22:07:50 +03:00
|
|
|
int UDPSocket::protocol_receive(const KBuffer& packet_buffer, void* buffer, size_t buffer_size, int flags)
|
2019-03-14 14:43:18 +03:00
|
|
|
{
|
|
|
|
(void)flags;
|
2019-08-04 22:07:50 +03:00
|
|
|
auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.data());
|
2019-03-14 14:43:18 +03:00
|
|
|
auto& udp_packet = *static_cast<const UDPPacket*>(ipv4_packet.payload());
|
|
|
|
ASSERT(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier.
|
|
|
|
ASSERT(buffer_size >= (udp_packet.length() - sizeof(UDPPacket)));
|
|
|
|
memcpy(buffer, udp_packet.payload(), udp_packet.length() - sizeof(UDPPacket));
|
|
|
|
return udp_packet.length() - sizeof(UDPPacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
int UDPSocket::protocol_send(const void* data, int data_length)
|
|
|
|
{
|
2019-08-28 14:58:01 +03:00
|
|
|
auto routing_decision = route_to(peer_address(), local_address());
|
2019-08-29 04:18:38 +03:00
|
|
|
if (routing_decision.is_zero())
|
2019-04-02 16:46:44 +03:00
|
|
|
return -EHOSTUNREACH;
|
2019-03-14 14:43:18 +03:00
|
|
|
auto buffer = ByteBuffer::create_zeroed(sizeof(UDPPacket) + data_length);
|
|
|
|
auto& udp_packet = *(UDPPacket*)(buffer.pointer());
|
2019-05-04 17:40:34 +03:00
|
|
|
udp_packet.set_source_port(local_port());
|
|
|
|
udp_packet.set_destination_port(peer_port());
|
2019-03-14 14:43:18 +03:00
|
|
|
udp_packet.set_length(sizeof(UDPPacket) + data_length);
|
|
|
|
memcpy(udp_packet.payload(), data, data_length);
|
|
|
|
kprintf("sending as udp packet from %s:%u to %s:%u!\n",
|
2019-08-28 14:58:01 +03:00
|
|
|
routing_decision.adapter->ipv4_address().to_string().characters(),
|
2019-05-04 17:40:34 +03:00
|
|
|
local_port(),
|
|
|
|
peer_address().to_string().characters(),
|
|
|
|
peer_port());
|
2019-09-19 22:40:06 +03:00
|
|
|
routing_decision.adapter->send_ipv4(routing_decision.next_hop, peer_address(), IPv4Protocol::UDP, buffer.data(), buffer.size(), ttl());
|
2019-03-14 14:43:18 +03:00
|
|
|
return data_length;
|
|
|
|
}
|
|
|
|
|
2019-08-11 16:38:20 +03:00
|
|
|
KResult UDPSocket::protocol_connect(FileDescription&, ShouldBlock)
|
|
|
|
{
|
|
|
|
m_role = Role::Connected;
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2019-05-04 17:40:34 +03:00
|
|
|
int UDPSocket::protocol_allocate_local_port()
|
2019-03-14 14:43:18 +03:00
|
|
|
{
|
2019-07-03 22:17:35 +03: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;
|
|
|
|
u16 first_scan_port = first_ephemeral_port + RandomDevice::random_value() % ephemeral_port_range_size;
|
2019-03-18 06:03:44 +03:00
|
|
|
|
2019-03-14 14:43:18 +03:00
|
|
|
LOCKER(sockets_by_port().lock());
|
2019-07-03 22:17:35 +03:00
|
|
|
for (u16 port = first_scan_port;;) {
|
2019-03-14 14:43:18 +03:00
|
|
|
auto it = sockets_by_port().resource().find(port);
|
|
|
|
if (it == sockets_by_port().resource().end()) {
|
2019-05-04 17:40:34 +03:00
|
|
|
set_local_port(port);
|
2019-03-14 14:43:18 +03:00
|
|
|
sockets_by_port().resource().set(port, this);
|
2019-03-18 06:03:44 +03:00
|
|
|
return port;
|
2019-03-14 14:43:18 +03:00
|
|
|
}
|
2019-03-18 06:03:44 +03:00
|
|
|
++port;
|
|
|
|
if (port > last_ephemeral_port)
|
|
|
|
port = first_ephemeral_port;
|
|
|
|
if (port == first_scan_port)
|
|
|
|
break;
|
2019-03-14 14:43:18 +03:00
|
|
|
}
|
2019-03-18 06:03:44 +03:00
|
|
|
return -EADDRINUSE;
|
2019-03-14 14:43:18 +03:00
|
|
|
}
|
2019-05-03 22:51:40 +03:00
|
|
|
|
|
|
|
KResult UDPSocket::protocol_bind()
|
|
|
|
{
|
|
|
|
LOCKER(sockets_by_port().lock());
|
2019-05-04 17:40:34 +03:00
|
|
|
if (sockets_by_port().resource().contains(local_port()))
|
2019-05-03 22:51:40 +03:00
|
|
|
return KResult(-EADDRINUSE);
|
2019-05-04 17:40:34 +03:00
|
|
|
sockets_by_port().resource().set(local_port(), this);
|
2019-05-03 22:51:40 +03:00
|
|
|
return KSuccess;
|
|
|
|
}
|