Serenity Operating System
at master 145 lines 5.8 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#pragma once 8 9#include <AK/HashMap.h> 10#include <AK/SinglyLinkedList.h> 11#include <Kernel/DoubleBuffer.h> 12#include <Kernel/KBuffer.h> 13#include <Kernel/Locking/MutexProtected.h> 14#include <Kernel/Net/IPv4.h> 15#include <Kernel/Net/IPv4SocketTuple.h> 16#include <Kernel/Net/Socket.h> 17 18namespace Kernel { 19 20class NetworkAdapter; 21class TCPPacket; 22class TCPSocket; 23 24struct PortAllocationResult { 25 ErrorOr<u16> error_or_port; 26 bool did_allocate; 27}; 28 29class IPv4Socket : public Socket { 30public: 31 static ErrorOr<NonnullRefPtr<Socket>> create(int type, int protocol); 32 virtual ~IPv4Socket() override; 33 34 virtual ErrorOr<void> close() override; 35 virtual ErrorOr<void> bind(Credentials const&, Userspace<sockaddr const*>, socklen_t) override; 36 virtual ErrorOr<void> connect(Credentials const&, OpenFileDescription&, Userspace<sockaddr const*>, socklen_t) override; 37 virtual ErrorOr<void> listen(size_t) override; 38 virtual void get_local_address(sockaddr*, socklen_t*) override; 39 virtual void get_peer_address(sockaddr*, socklen_t*) override; 40 virtual bool can_read(OpenFileDescription const&, u64) const override; 41 virtual bool can_write(OpenFileDescription const&, u64) const override; 42 virtual ErrorOr<size_t> sendto(OpenFileDescription&, UserOrKernelBuffer const&, size_t, int, Userspace<sockaddr const*>, socklen_t) override; 43 virtual ErrorOr<size_t> recvfrom(OpenFileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, Time&, bool blocking) override; 44 virtual ErrorOr<void> setsockopt(int level, int option, Userspace<void const*>, socklen_t) override; 45 virtual ErrorOr<void> getsockopt(OpenFileDescription&, int level, int option, Userspace<void*>, Userspace<socklen_t*>) override; 46 47 virtual ErrorOr<void> ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg) override; 48 49 bool did_receive(IPv4Address const& peer_address, u16 peer_port, ReadonlyBytes, Time const&); 50 51 IPv4Address const& local_address() const { return m_local_address; } 52 u16 local_port() const { return m_local_port; } 53 void set_local_port(u16 port) { m_local_port = port; } 54 bool has_specific_local_address() { return m_local_address.to_u32() != 0; } 55 56 IPv4Address const& peer_address() const { return m_peer_address; } 57 u16 peer_port() const { return m_peer_port; } 58 void set_peer_port(u16 port) { m_peer_port = port; } 59 60 Vector<IPv4Address> const& multicast_memberships() const { return m_multicast_memberships; } 61 62 IPv4SocketTuple tuple() const { return IPv4SocketTuple(m_local_address, m_local_port, m_peer_address, m_peer_port); } 63 64 ErrorOr<NonnullOwnPtr<KString>> pseudo_path(OpenFileDescription const& description) const override; 65 66 u8 type_of_service() const { return m_type_of_service; } 67 u8 ttl() const { return m_ttl; } 68 69 enum class BufferMode { 70 Packets, 71 Bytes, 72 }; 73 BufferMode buffer_mode() const { return m_buffer_mode; } 74 75protected: 76 IPv4Socket(int type, int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer, OwnPtr<KBuffer> optional_scratch_buffer); 77 virtual StringView class_name() const override { return "IPv4Socket"sv; } 78 79 PortAllocationResult allocate_local_port_if_needed(); 80 81 virtual ErrorOr<void> protocol_bind() { return {}; } 82 virtual ErrorOr<void> protocol_listen([[maybe_unused]] bool did_allocate_port) { return {}; } 83 virtual ErrorOr<size_t> protocol_receive(ReadonlyBytes /* raw_ipv4_packet */, UserOrKernelBuffer&, size_t, int) { return ENOTIMPL; } 84 virtual ErrorOr<size_t> protocol_send(UserOrKernelBuffer const&, size_t) { return ENOTIMPL; } 85 virtual ErrorOr<void> protocol_connect(OpenFileDescription&) { return {}; } 86 virtual ErrorOr<u16> protocol_allocate_local_port() { return ENOPROTOOPT; } 87 virtual ErrorOr<size_t> protocol_size(ReadonlyBytes /* raw_ipv4_packet */) { return ENOTIMPL; } 88 virtual bool protocol_is_disconnected() const { return false; } 89 90 virtual void shut_down_for_reading() override; 91 92 void set_local_address(IPv4Address address) { m_local_address = address; } 93 void set_peer_address(IPv4Address address) { m_peer_address = address; } 94 95 static ErrorOr<NonnullOwnPtr<DoubleBuffer>> try_create_receive_buffer(); 96 void drop_receive_buffer(); 97 98private: 99 virtual bool is_ipv4() const override { return true; } 100 101 ErrorOr<size_t> receive_byte_buffered(OpenFileDescription&, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, bool blocking); 102 ErrorOr<size_t> receive_packet_buffered(OpenFileDescription&, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, Time&, bool blocking); 103 104 void set_can_read(bool); 105 106 IPv4Address m_local_address; 107 IPv4Address m_peer_address; 108 109 Vector<IPv4Address> m_multicast_memberships; 110 bool m_multicast_loop { true }; 111 112 struct ReceivedPacket { 113 IPv4Address peer_address; 114 u16 peer_port; 115 Time timestamp; 116 OwnPtr<KBuffer> data; 117 }; 118 119 SinglyLinkedList<ReceivedPacket, CountingSizeCalculationPolicy> m_receive_queue; 120 121 OwnPtr<DoubleBuffer> m_receive_buffer; 122 123 u16 m_local_port { 0 }; 124 u16 m_peer_port { 0 }; 125 126 u32 m_bytes_received { 0 }; 127 128 u8 m_type_of_service { IPTOS_LOWDELAY }; 129 u8 m_ttl { 64 }; 130 131 bool m_can_read { false }; 132 133 BufferMode m_buffer_mode { BufferMode::Packets }; 134 135 OwnPtr<KBuffer> m_scratch_buffer; 136 137 IntrusiveListNode<IPv4Socket> m_list_node; 138 139public: 140 using List = IntrusiveList<&IPv4Socket::m_list_node>; 141 142 static MutexProtected<IPv4Socket::List>& all_sockets(); 143}; 144 145}