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-08-04 21:07:50 +02:00
|
|
|
#include <Kernel/KBuffer.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <Kernel/DoubleBuffer.h>
|
|
|
|
#include <Kernel/Lock.h>
|
|
|
|
#include <Kernel/Net/IPv4.h>
|
|
|
|
#include <Kernel/Net/Socket.h>
|
2019-03-12 15:51:42 +01:00
|
|
|
|
2019-03-14 09:19:24 +01:00
|
|
|
class IPv4SocketHandle;
|
2019-03-14 12:20:38 +01:00
|
|
|
class TCPSocketHandle;
|
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:
|
2019-06-21 18:37:47 +02:00
|
|
|
static NonnullRefPtr<IPv4Socket> 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();
|
|
|
|
|
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;
|
2019-05-20 20:33:03 +02:00
|
|
|
virtual bool get_local_address(sockaddr*, socklen_t*) override;
|
|
|
|
virtual bool 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;
|
|
|
|
virtual bool can_read(FileDescription&) const override;
|
|
|
|
virtual bool can_write(FileDescription&) const override;
|
|
|
|
virtual ssize_t sendto(FileDescription&, const void*, size_t, int, const sockaddr*, socklen_t) override;
|
|
|
|
virtual ssize_t recvfrom(FileDescription&, void*, size_t, int flags, sockaddr*, socklen_t*) override;
|
2019-03-12 17:27:07 +01:00
|
|
|
|
2019-08-05 11:06:21 +02:00
|
|
|
void did_receive(const IPv4Address& peer_address, u16 peer_port, KBuffer&&);
|
2019-03-12 17:27:07 +01:00
|
|
|
|
2019-05-04 16:40:34 +02:00
|
|
|
const IPv4Address& local_address() const;
|
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-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-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-04 21:07:50 +02:00
|
|
|
virtual int protocol_receive(const KBuffer&, void*, size_t, int) { return -ENOTIMPL; }
|
2019-03-14 12:20:38 +01:00
|
|
|
virtual int protocol_send(const void*, int) { 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
|
|
|
|
2019-03-12 15:51:42 +01:00
|
|
|
private:
|
|
|
|
virtual bool is_ipv4() const override { return true; }
|
|
|
|
|
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-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-03-12 17:27:07 +01:00
|
|
|
bool m_can_read { false };
|
2019-03-12 15:51:42 +01:00
|
|
|
};
|
|
|
|
|
2019-03-14 09:19:24 +01:00
|
|
|
class IPv4SocketHandle : public SocketHandle {
|
|
|
|
public:
|
2019-05-28 11:53:16 +02:00
|
|
|
IPv4SocketHandle() {}
|
2019-03-14 09:19:24 +01:00
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
IPv4SocketHandle(RefPtr<IPv4Socket>&& socket)
|
2019-03-14 09:19:24 +01:00
|
|
|
: 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()); }
|
|
|
|
};
|