2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-03-12 15:51:42 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-13 16:05:56 +01:00
|
|
|
#include <AK/HashMap.h>
|
2020-08-04 21:35:30 -07:00
|
|
|
#include <AK/SinglyLinkedListWithCount.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <Kernel/DoubleBuffer.h>
|
2019-08-06 23:40:38 +10:00
|
|
|
#include <Kernel/KBuffer.h>
|
2021-08-21 23:31:15 +02:00
|
|
|
#include <Kernel/Locking/MutexProtected.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <Kernel/Net/IPv4.h>
|
2019-08-06 23:40:38 +10:00
|
|
|
#include <Kernel/Net/IPv4SocketTuple.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <Kernel/Net/Socket.h>
|
2019-03-12 15:51:42 +01:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-03-13 23:14:30 +01:00
|
|
|
class NetworkAdapter;
|
|
|
|
class TCPPacket;
|
2019-03-14 12:20:38 +01:00
|
|
|
class TCPSocket;
|
2019-03-13 23:14:30 +01:00
|
|
|
|
2021-06-01 01:11:14 +02:00
|
|
|
struct PortAllocationResult {
|
|
|
|
KResultOr<u16> error_or_port;
|
|
|
|
bool did_allocate;
|
|
|
|
};
|
|
|
|
|
2019-03-14 12:20:38 +01:00
|
|
|
class IPv4Socket : public Socket {
|
2019-03-12 15:51:42 +01:00
|
|
|
public:
|
2020-01-23 18:11:14 +01:00
|
|
|
static KResultOr<NonnullRefPtr<Socket>> create(int type, int protocol);
|
2019-03-12 15:51:42 +01:00
|
|
|
virtual ~IPv4Socket() override;
|
|
|
|
|
2020-06-02 19:20:05 +03:00
|
|
|
virtual KResult close() override;
|
2020-08-09 15:13:43 -07:00
|
|
|
virtual KResult bind(Userspace<const sockaddr*>, socklen_t) override;
|
2020-09-11 21:11:07 -06:00
|
|
|
virtual KResult connect(FileDescription&, Userspace<const sockaddr*>, socklen_t, ShouldBlock = ShouldBlock::Yes) override;
|
2020-02-25 14:49:47 +01:00
|
|
|
virtual KResult listen(size_t) override;
|
2020-02-07 23:42:28 +01:00
|
|
|
virtual void get_local_address(sockaddr*, socklen_t*) override;
|
|
|
|
virtual void get_peer_address(sockaddr*, socklen_t*) override;
|
2020-04-10 19:44:42 +10:00
|
|
|
virtual bool can_read(const FileDescription&, size_t) const override;
|
|
|
|
virtual bool can_write(const FileDescription&, size_t) const override;
|
2020-09-11 21:11:07 -06:00
|
|
|
virtual KResultOr<size_t> sendto(FileDescription&, const UserOrKernelBuffer&, size_t, int, Userspace<const sockaddr*>, socklen_t) override;
|
2021-02-28 02:48:45 +01:00
|
|
|
virtual KResultOr<size_t> recvfrom(FileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, Time&) override;
|
2020-08-07 00:18:20 -07:00
|
|
|
virtual KResult setsockopt(int level, int option, Userspace<const void*>, socklen_t) override;
|
2020-08-07 02:29:05 -07:00
|
|
|
virtual KResult getsockopt(FileDescription&, int level, int option, Userspace<void*>, Userspace<socklen_t*>) override;
|
2019-03-12 17:27:07 +01:00
|
|
|
|
2021-07-26 03:47:25 -07:00
|
|
|
virtual KResult ioctl(FileDescription&, unsigned request, Userspace<void*> arg) override;
|
2019-09-23 19:06:03 +02:00
|
|
|
|
2021-05-11 19:47:40 +02:00
|
|
|
bool did_receive(const IPv4Address& peer_address, u16 peer_port, ReadonlyBytes, const Time&);
|
2019-03-12 17:27:07 +01:00
|
|
|
|
2019-08-06 23:40:38 +10:00
|
|
|
const IPv4Address& local_address() const { return m_local_address; }
|
2019-07-03 21:17:35 +02:00
|
|
|
u16 local_port() const { return m_local_port; }
|
|
|
|
void set_local_port(u16 port) { m_local_port = port; }
|
2019-08-09 12:38:56 +10:00
|
|
|
bool has_specific_local_address() { return m_local_address.to_u32() != 0; }
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2019-05-04 16:40:34 +02:00
|
|
|
const IPv4Address& peer_address() const { return m_peer_address; }
|
2019-07-03 21:17:35 +02:00
|
|
|
u16 peer_port() const { return m_peer_port; }
|
|
|
|
void set_peer_port(u16 port) { m_peer_port = port; }
|
2019-03-13 14:22:27 +01:00
|
|
|
|
2021-05-04 14:42:47 +03:00
|
|
|
const Vector<IPv4Address>& multicast_memberships() const { return m_multicast_memberships; }
|
|
|
|
|
2019-08-06 23:40:38 +10:00
|
|
|
IPv4SocketTuple tuple() const { return IPv4SocketTuple(m_local_address, m_local_port, m_peer_address, m_peer_port); }
|
|
|
|
|
2019-08-10 19:10:36 +03:00
|
|
|
String absolute_path(const FileDescription& description) const override;
|
|
|
|
|
2019-09-19 21:40:06 +02:00
|
|
|
u8 ttl() const { return m_ttl; }
|
|
|
|
|
2019-12-14 09:43:31 +01:00
|
|
|
enum class BufferMode {
|
|
|
|
Packets,
|
|
|
|
Bytes,
|
|
|
|
};
|
|
|
|
BufferMode buffer_mode() const { return m_buffer_mode; }
|
|
|
|
|
2019-03-14 12:20:38 +01:00
|
|
|
protected:
|
2021-08-01 05:11:05 -07:00
|
|
|
IPv4Socket(int type, int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer, OwnPtr<KBuffer> optional_scratch_buffer);
|
2021-07-11 01:46:09 +02:00
|
|
|
virtual StringView class_name() const override { return "IPv4Socket"; }
|
2019-03-14 12:28:30 +01:00
|
|
|
|
2021-06-01 01:11:14 +02:00
|
|
|
PortAllocationResult allocate_local_port_if_needed();
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2019-05-03 21:51:40 +02:00
|
|
|
virtual KResult protocol_bind() { return KSuccess; }
|
2021-06-01 01:11:14 +02:00
|
|
|
virtual KResult protocol_listen([[maybe_unused]] bool did_allocate_port) { return KSuccess; }
|
2021-04-30 12:29:30 +02:00
|
|
|
virtual KResultOr<size_t> protocol_receive(ReadonlyBytes /* raw_ipv4_packet */, UserOrKernelBuffer&, size_t, int) { return ENOTIMPL; }
|
|
|
|
virtual KResultOr<size_t> protocol_send(const UserOrKernelBuffer&, size_t) { return ENOTIMPL; }
|
2019-06-07 09:36:51 +02:00
|
|
|
virtual KResult protocol_connect(FileDescription&, ShouldBlock) { return KSuccess; }
|
2021-04-30 12:26:25 +02:00
|
|
|
virtual KResultOr<u16> protocol_allocate_local_port() { return ENOPROTOOPT; }
|
2019-03-14 15:23:32 +01:00
|
|
|
virtual bool protocol_is_disconnected() const { return false; }
|
2019-03-13 23:14:30 +01:00
|
|
|
|
2020-02-08 15:59:21 +01:00
|
|
|
virtual void shut_down_for_reading() override;
|
|
|
|
|
2019-08-06 23:40:38 +10:00
|
|
|
void set_local_address(IPv4Address address) { m_local_address = address; }
|
|
|
|
void set_peer_address(IPv4Address address) { m_peer_address = address; }
|
|
|
|
|
2021-08-01 02:42:03 -07:00
|
|
|
static OwnPtr<DoubleBuffer> create_receive_buffer();
|
|
|
|
|
2019-03-12 15:51:42 +01:00
|
|
|
private:
|
|
|
|
virtual bool is_ipv4() const override { return true; }
|
|
|
|
|
2020-09-11 21:11:07 -06:00
|
|
|
KResultOr<size_t> receive_byte_buffered(FileDescription&, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>);
|
2021-02-28 02:48:45 +01:00
|
|
|
KResultOr<size_t> receive_packet_buffered(FileDescription&, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, Time&);
|
2020-02-08 13:09:37 +01:00
|
|
|
|
2020-11-29 16:05:27 -07:00
|
|
|
void set_can_read(bool);
|
|
|
|
|
2019-05-04 16:40:34 +02:00
|
|
|
IPv4Address m_local_address;
|
|
|
|
IPv4Address m_peer_address;
|
2019-03-12 15:51:42 +01:00
|
|
|
|
2021-05-04 14:42:47 +03:00
|
|
|
Vector<IPv4Address> m_multicast_memberships;
|
|
|
|
bool m_multicast_loop { true };
|
|
|
|
|
2019-05-04 03:27:50 +02:00
|
|
|
struct ReceivedPacket {
|
2019-05-04 16:40:34 +02:00
|
|
|
IPv4Address peer_address;
|
2019-07-03 21:17:35 +02:00
|
|
|
u16 peer_port;
|
2021-02-28 02:48:45 +01:00
|
|
|
Time timestamp;
|
2019-08-05 11:06:21 +02:00
|
|
|
Optional<KBuffer> data;
|
2019-05-04 03:27:50 +02:00
|
|
|
};
|
|
|
|
|
2020-08-04 21:35:30 -07:00
|
|
|
SinglyLinkedListWithCount<ReceivedPacket> m_receive_queue;
|
2019-03-12 17:27:07 +01:00
|
|
|
|
2021-08-01 02:42:03 -07:00
|
|
|
NonnullOwnPtr<DoubleBuffer> m_receive_buffer;
|
2019-12-14 09:43:31 +01:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
u16 m_local_port { 0 };
|
|
|
|
u16 m_peer_port { 0 };
|
2019-03-13 14:22:27 +01:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
u32 m_bytes_received { 0 };
|
2019-03-14 15:28:23 +01:00
|
|
|
|
2019-09-19 21:40:06 +02:00
|
|
|
u8 m_ttl { 64 };
|
|
|
|
|
2019-03-12 17:27:07 +01:00
|
|
|
bool m_can_read { false };
|
2019-12-14 09:43:31 +01:00
|
|
|
|
|
|
|
BufferMode m_buffer_mode { BufferMode::Packets };
|
|
|
|
|
2021-08-01 05:11:05 -07:00
|
|
|
OwnPtr<KBuffer> m_scratch_buffer;
|
2021-08-15 15:46:35 +02:00
|
|
|
|
|
|
|
IntrusiveListNode<IPv4Socket> m_list_node;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using List = IntrusiveList<IPv4Socket, RawPtr<IPv4Socket>, &IPv4Socket::m_list_node>;
|
|
|
|
|
2021-08-21 23:31:15 +02:00
|
|
|
static MutexProtected<IPv4Socket::List>& all_sockets();
|
2019-03-12 15:51:42 +01:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|