2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-03-12 15:51:42 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-13 16:05:56 +01:00
|
|
|
#include <AK/HashMap.h>
|
2019-03-12 17:27:07 +01:00
|
|
|
#include <AK/SinglyLinkedList.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>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <Kernel/Lock.h>
|
|
|
|
#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
|
|
|
|
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;
|
|
|
|
|
2019-03-12 17:27:07 +01:00
|
|
|
static Lockable<HashTable<IPv4Socket*>>& all_sockets();
|
|
|
|
|
2020-06-02 19:20:05 +03:00
|
|
|
virtual KResult close() override;
|
2019-03-12 15:51:42 +01:00
|
|
|
virtual KResult bind(const sockaddr*, socklen_t) override;
|
2019-06-07 09:36:51 +02:00
|
|
|
virtual KResult connect(FileDescription&, 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;
|
2019-06-07 09:36:51 +02:00
|
|
|
virtual void attach(FileDescription&) override;
|
|
|
|
virtual void detach(FileDescription&) 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-08-04 18:02:23 +02:00
|
|
|
virtual KResultOr<size_t> sendto(FileDescription&, const void*, size_t, int, const sockaddr*, socklen_t) override;
|
|
|
|
virtual KResultOr<size_t> recvfrom(FileDescription&, void*, size_t, int flags, sockaddr*, socklen_t*) override;
|
2019-09-19 21:40:06 +02:00
|
|
|
virtual KResult setsockopt(int level, int option, const void*, socklen_t) override;
|
2019-12-06 18:38:36 +01:00
|
|
|
virtual KResult getsockopt(FileDescription&, int level, int option, void*, socklen_t*) override;
|
2019-03-12 17:27:07 +01:00
|
|
|
|
2020-05-23 13:17:58 +02:00
|
|
|
virtual int ioctl(FileDescription&, unsigned request, FlatPtr arg) override;
|
2019-09-23 19:06:03 +02:00
|
|
|
|
2019-11-04 14:03:14 +01:00
|
|
|
bool did_receive(const IPv4Address& peer_address, u16 peer_port, KBuffer&&);
|
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
|
|
|
|
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:
|
|
|
|
IPv4Socket(int type, int protocol);
|
2019-05-03 20:42:43 +02:00
|
|
|
virtual const char* class_name() const override { return "IPv4Socket"; }
|
2019-03-14 12:28:30 +01:00
|
|
|
|
2019-05-04 16:40:34 +02:00
|
|
|
int 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; }
|
2019-08-06 23:40:38 +10:00
|
|
|
virtual KResult protocol_listen() { return KSuccess; }
|
2020-08-04 18:02:23 +02:00
|
|
|
virtual KResultOr<size_t> protocol_receive(const KBuffer&, void*, size_t, int) { return -ENOTIMPL; }
|
|
|
|
virtual KResultOr<size_t> protocol_send(const void*, size_t) { return -ENOTIMPL; }
|
2019-06-07 09:36:51 +02:00
|
|
|
virtual KResult protocol_connect(FileDescription&, ShouldBlock) { return KSuccess; }
|
2019-05-04 16:40:34 +02:00
|
|
|
virtual int protocol_allocate_local_port() { return 0; }
|
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; }
|
|
|
|
|
2019-03-12 15:51:42 +01:00
|
|
|
private:
|
|
|
|
virtual bool is_ipv4() const override { return true; }
|
|
|
|
|
2020-08-04 18:02:23 +02:00
|
|
|
KResultOr<size_t> receive_byte_buffered(FileDescription&, void* buffer, size_t buffer_length, int flags, sockaddr*, socklen_t*);
|
|
|
|
KResultOr<size_t> receive_packet_buffered(FileDescription&, void* buffer, size_t buffer_length, int flags, sockaddr*, socklen_t*);
|
2020-02-08 13:09:37 +01:00
|
|
|
|
2019-05-04 16:40:34 +02:00
|
|
|
IPv4Address m_local_address;
|
|
|
|
IPv4Address m_peer_address;
|
2019-03-12 15:51:42 +01:00
|
|
|
|
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;
|
2019-08-05 11:06:21 +02:00
|
|
|
Optional<KBuffer> data;
|
2019-05-04 03:27:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
SinglyLinkedList<ReceivedPacket> m_receive_queue;
|
2019-03-12 17:27:07 +01:00
|
|
|
|
2019-12-14 09:43:31 +01:00
|
|
|
DoubleBuffer m_receive_buffer;
|
|
|
|
|
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 };
|
|
|
|
|
|
|
|
Optional<KBuffer> m_scratch_buffer;
|
2019-03-12 15:51:42 +01:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|