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