2019-03-12 17:51:42 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-06 21:29:48 +03:00
|
|
|
#include <Kernel/Net/Socket.h>
|
2019-03-12 17:51:42 +03:00
|
|
|
#include <Kernel/DoubleBuffer.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Net/IPv4.h>
|
2019-03-13 18:05:56 +03:00
|
|
|
#include <AK/HashMap.h>
|
2019-03-16 15:18:22 +03:00
|
|
|
#include <Kernel/Lock.h>
|
2019-03-12 19:27:07 +03:00
|
|
|
#include <AK/SinglyLinkedList.h>
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2019-03-14 11:19:24 +03:00
|
|
|
class IPv4SocketHandle;
|
2019-03-14 14:20:38 +03:00
|
|
|
class TCPSocketHandle;
|
2019-03-14 01:14:30 +03:00
|
|
|
class NetworkAdapter;
|
|
|
|
class TCPPacket;
|
2019-03-14 14:20:38 +03:00
|
|
|
class TCPSocket;
|
2019-03-14 01:14:30 +03:00
|
|
|
|
2019-03-14 14:20:38 +03:00
|
|
|
class IPv4Socket : public Socket {
|
2019-03-12 17:51:42 +03:00
|
|
|
public:
|
|
|
|
static Retained<IPv4Socket> create(int type, int protocol);
|
|
|
|
virtual ~IPv4Socket() override;
|
|
|
|
|
2019-03-12 19:27:07 +03:00
|
|
|
static Lockable<HashTable<IPv4Socket*>>& all_sockets();
|
|
|
|
|
2019-03-12 17:51:42 +03:00
|
|
|
virtual KResult bind(const sockaddr*, socklen_t) override;
|
2019-05-03 21:15:54 +03:00
|
|
|
virtual KResult connect(FileDescriptor&, const sockaddr*, socklen_t, ShouldBlock = ShouldBlock::Yes) override;
|
2019-03-12 17:51:42 +03:00
|
|
|
virtual bool get_address(sockaddr*, socklen_t*) override;
|
2019-05-03 21:15:54 +03:00
|
|
|
virtual void attach(FileDescriptor&) override;
|
|
|
|
virtual void detach(FileDescriptor&) override;
|
|
|
|
virtual bool can_read(FileDescriptor&) const override;
|
|
|
|
virtual ssize_t read(FileDescriptor&, byte*, ssize_t) override;
|
|
|
|
virtual ssize_t write(FileDescriptor&, const byte*, ssize_t) override;
|
|
|
|
virtual bool can_write(FileDescriptor&) const override;
|
|
|
|
virtual ssize_t sendto(FileDescriptor&, const void*, size_t, int, const sockaddr*, socklen_t) override;
|
|
|
|
virtual ssize_t recvfrom(FileDescriptor&, void*, size_t, int flags, sockaddr*, socklen_t*) override;
|
2019-03-12 19:27:07 +03:00
|
|
|
|
|
|
|
void did_receive(ByteBuffer&&);
|
|
|
|
|
2019-03-14 14:20:38 +03:00
|
|
|
const IPv4Address& source_address() const;
|
2019-03-13 16:22:27 +03:00
|
|
|
word source_port() const { return m_source_port; }
|
2019-03-14 14:28:30 +03:00
|
|
|
void set_source_port(word port) { m_source_port = port; }
|
2019-03-14 14:20:38 +03:00
|
|
|
|
|
|
|
const IPv4Address& destination_address() const { return m_destination_address; }
|
2019-03-13 16:22:27 +03:00
|
|
|
word destination_port() const { return m_destination_port; }
|
2019-03-14 14:28:30 +03:00
|
|
|
void set_destination_port(word port) { m_destination_port = port; }
|
2019-03-13 16:22:27 +03:00
|
|
|
|
2019-03-14 14:20:38 +03:00
|
|
|
protected:
|
|
|
|
IPv4Socket(int type, int protocol);
|
2019-05-03 21:42:43 +03:00
|
|
|
virtual const char* class_name() const override { return "IPv4Socket"; }
|
2019-03-14 14:28:30 +03:00
|
|
|
|
2019-03-18 06:03:44 +03:00
|
|
|
int allocate_source_port_if_needed();
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2019-05-03 22:51:40 +03:00
|
|
|
virtual KResult protocol_bind() { return KSuccess; }
|
2019-03-14 14:20:38 +03:00
|
|
|
virtual int protocol_receive(const ByteBuffer&, void*, size_t, int, sockaddr*, socklen_t*) { return -ENOTIMPL; }
|
|
|
|
virtual int protocol_send(const void*, int) { return -ENOTIMPL; }
|
2019-05-03 21:15:54 +03:00
|
|
|
virtual KResult protocol_connect(FileDescriptor&, ShouldBlock) { return KSuccess; }
|
2019-03-18 06:03:44 +03:00
|
|
|
virtual int protocol_allocate_source_port() { return 0; }
|
2019-03-14 17:23:32 +03:00
|
|
|
virtual bool protocol_is_disconnected() const { return false; }
|
2019-03-14 01:14:30 +03:00
|
|
|
|
2019-03-12 17:51:42 +03:00
|
|
|
private:
|
|
|
|
virtual bool is_ipv4() const override { return true; }
|
|
|
|
|
|
|
|
bool m_bound { false };
|
|
|
|
int m_attached_fds { 0 };
|
2019-05-03 22:51:40 +03:00
|
|
|
|
|
|
|
IPv4Address m_source_address;
|
2019-03-13 16:47:21 +03:00
|
|
|
IPv4Address m_destination_address;
|
2019-03-12 17:51:42 +03:00
|
|
|
|
|
|
|
DoubleBuffer m_for_client;
|
|
|
|
DoubleBuffer m_for_server;
|
2019-03-12 19:27:07 +03:00
|
|
|
|
|
|
|
SinglyLinkedList<ByteBuffer> m_receive_queue;
|
|
|
|
|
2019-03-13 16:22:27 +03:00
|
|
|
word m_source_port { 0 };
|
|
|
|
word m_destination_port { 0 };
|
|
|
|
|
2019-03-14 17:28:23 +03:00
|
|
|
dword m_bytes_received { 0 };
|
|
|
|
|
2019-03-12 19:27:07 +03:00
|
|
|
bool m_can_read { false };
|
2019-03-12 17:51:42 +03:00
|
|
|
};
|
|
|
|
|
2019-03-14 11:19:24 +03:00
|
|
|
class IPv4SocketHandle : public SocketHandle {
|
|
|
|
public:
|
|
|
|
IPv4SocketHandle() { }
|
|
|
|
|
|
|
|
IPv4SocketHandle(RetainPtr<IPv4Socket>&& socket)
|
|
|
|
: SocketHandle(move(socket))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IPv4SocketHandle(IPv4SocketHandle&& other)
|
|
|
|
: SocketHandle(move(other))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IPv4SocketHandle(const IPv4SocketHandle&) = delete;
|
|
|
|
IPv4SocketHandle& operator=(const IPv4SocketHandle&) = delete;
|
|
|
|
|
|
|
|
IPv4Socket* operator->() { return &socket(); }
|
|
|
|
const IPv4Socket* operator->() const { return &socket(); }
|
|
|
|
|
|
|
|
IPv4Socket& socket() { return static_cast<IPv4Socket&>(SocketHandle::socket()); }
|
|
|
|
const IPv4Socket& socket() const { return static_cast<const IPv4Socket&>(SocketHandle::socket()); }
|
|
|
|
};
|