Serenity Operating System
at hosted 143 lines 5.7 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29#include <AK/HashMap.h> 30#include <AK/SinglyLinkedList.h> 31#include <Kernel/DoubleBuffer.h> 32#include <Kernel/KBuffer.h> 33#include <Kernel/Lock.h> 34#include <Kernel/Net/IPv4.h> 35#include <Kernel/Net/IPv4SocketTuple.h> 36#include <Kernel/Net/Socket.h> 37 38namespace Kernel { 39 40class NetworkAdapter; 41class TCPPacket; 42class TCPSocket; 43 44class IPv4Socket : public Socket { 45public: 46 static KResultOr<NonnullRefPtr<Socket>> create(int type, int protocol); 47 virtual ~IPv4Socket() override; 48 49 static Lockable<HashTable<IPv4Socket*>>& all_sockets(); 50 51 virtual void close() override; 52 virtual KResult bind(const sockaddr*, socklen_t) override; 53 virtual KResult connect(FileDescription&, const sockaddr*, socklen_t, ShouldBlock = ShouldBlock::Yes) override; 54 virtual KResult listen(size_t) override; 55 virtual void get_local_address(sockaddr*, socklen_t*) override; 56 virtual void get_peer_address(sockaddr*, socklen_t*) override; 57 virtual void attach(FileDescription&) override; 58 virtual void detach(FileDescription&) override; 59 virtual bool can_read(const FileDescription&) const override; 60 virtual bool can_write(const FileDescription&) const override; 61 virtual ssize_t sendto(FileDescription&, const void*, size_t, int, const sockaddr*, socklen_t) override; 62 virtual ssize_t recvfrom(FileDescription&, void*, size_t, int flags, sockaddr*, socklen_t*) override; 63 virtual KResult setsockopt(int level, int option, const void*, socklen_t) override; 64 virtual KResult getsockopt(FileDescription&, int level, int option, void*, socklen_t*) override; 65 66 virtual int ioctl(FileDescription&, unsigned request, unsigned arg) override; 67 68 bool did_receive(const IPv4Address& peer_address, u16 peer_port, KBuffer&&); 69 70 const IPv4Address& local_address() const { return m_local_address; } 71 u16 local_port() const { return m_local_port; } 72 void set_local_port(u16 port) { m_local_port = port; } 73 bool has_specific_local_address() { return m_local_address.to_u32() != 0; } 74 75 const IPv4Address& peer_address() const { return m_peer_address; } 76 u16 peer_port() const { return m_peer_port; } 77 void set_peer_port(u16 port) { m_peer_port = port; } 78 79 IPv4SocketTuple tuple() const { return IPv4SocketTuple(m_local_address, m_local_port, m_peer_address, m_peer_port); } 80 81 String absolute_path(const FileDescription& description) const override; 82 83 u8 ttl() const { return m_ttl; } 84 85 enum class BufferMode { 86 Packets, 87 Bytes, 88 }; 89 BufferMode buffer_mode() const { return m_buffer_mode; } 90 91protected: 92 IPv4Socket(int type, int protocol); 93 virtual const char* class_name() const override { return "IPv4Socket"; } 94 95 int allocate_local_port_if_needed(); 96 97 virtual KResult protocol_bind() { return KSuccess; } 98 virtual KResult protocol_listen() { return KSuccess; } 99 virtual int protocol_receive(const KBuffer&, void*, size_t, int) { return -ENOTIMPL; } 100 virtual int protocol_send(const void*, size_t) { return -ENOTIMPL; } 101 virtual KResult protocol_connect(FileDescription&, ShouldBlock) { return KSuccess; } 102 virtual int protocol_allocate_local_port() { return 0; } 103 virtual bool protocol_is_disconnected() const { return false; } 104 105 virtual void shut_down_for_reading() override; 106 107 void set_local_address(IPv4Address address) { m_local_address = address; } 108 void set_peer_address(IPv4Address address) { m_peer_address = address; } 109 110private: 111 virtual bool is_ipv4() const override { return true; } 112 113 ssize_t receive_byte_buffered(FileDescription&, void* buffer, size_t buffer_length, int flags, sockaddr*, socklen_t*); 114 ssize_t receive_packet_buffered(FileDescription&, void* buffer, size_t buffer_length, int flags, sockaddr*, socklen_t*); 115 116 IPv4Address m_local_address; 117 IPv4Address m_peer_address; 118 119 struct ReceivedPacket { 120 IPv4Address peer_address; 121 u16 peer_port; 122 Optional<KBuffer> data; 123 }; 124 125 SinglyLinkedList<ReceivedPacket> m_receive_queue; 126 127 DoubleBuffer m_receive_buffer; 128 129 u16 m_local_port { 0 }; 130 u16 m_peer_port { 0 }; 131 132 u32 m_bytes_received { 0 }; 133 134 u8 m_ttl { 64 }; 135 136 bool m_can_read { false }; 137 138 BufferMode m_buffer_mode { BufferMode::Packets }; 139 140 Optional<KBuffer> m_scratch_buffer; 141}; 142 143}