2019-03-14 12:20:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-08-06 23:40:38 +10:00
|
|
|
#include <AK/Function.h>
|
2019-04-02 19:54:38 +02:00
|
|
|
#include <Kernel/Net/IPv4Socket.h>
|
2019-03-14 12:20:38 +01:00
|
|
|
|
|
|
|
class TCPSocket final : public IPv4Socket {
|
|
|
|
public:
|
2019-08-06 23:40:38 +10:00
|
|
|
static void for_each(Function<void(TCPSocket*&)>);
|
2019-06-21 18:37:47 +02:00
|
|
|
static NonnullRefPtr<TCPSocket> create(int protocol);
|
2019-03-14 12:20:38 +01:00
|
|
|
virtual ~TCPSocket() override;
|
|
|
|
|
2019-06-07 17:13:23 +02:00
|
|
|
enum class State {
|
2019-08-06 23:40:38 +10:00
|
|
|
Closed,
|
|
|
|
Listen,
|
|
|
|
SynSent,
|
|
|
|
SynReceived,
|
|
|
|
Established,
|
|
|
|
CloseWait,
|
|
|
|
LastAck,
|
|
|
|
FinWait1,
|
|
|
|
FinWait2,
|
|
|
|
Closing,
|
|
|
|
TimeWait,
|
2019-03-14 12:20:38 +01:00
|
|
|
};
|
|
|
|
|
2019-08-06 23:40:38 +10:00
|
|
|
static const char* to_string(State state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case State::Closed:
|
|
|
|
return "Closed";
|
|
|
|
case State::Listen:
|
|
|
|
return "Listen";
|
|
|
|
case State::SynSent:
|
|
|
|
return "SynSent";
|
|
|
|
case State::SynReceived:
|
|
|
|
return "SynReceived";
|
|
|
|
case State::Established:
|
|
|
|
return "Established";
|
|
|
|
case State::CloseWait:
|
|
|
|
return "CloseWait";
|
|
|
|
case State::LastAck:
|
|
|
|
return "LastAck";
|
|
|
|
case State::FinWait1:
|
|
|
|
return "FinWait1";
|
|
|
|
case State::FinWait2:
|
|
|
|
return "FinWait2";
|
|
|
|
case State::Closing:
|
|
|
|
return "Closing";
|
|
|
|
case State::TimeWait:
|
|
|
|
return "TimeWait";
|
|
|
|
default:
|
|
|
|
return "None";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 12:20:38 +01:00
|
|
|
State state() const { return m_state; }
|
|
|
|
void set_state(State state) { m_state = state; }
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
void set_ack_number(u32 n) { m_ack_number = n; }
|
|
|
|
void set_sequence_number(u32 n) { m_sequence_number = n; }
|
|
|
|
u32 ack_number() const { return m_ack_number; }
|
|
|
|
u32 sequence_number() const { return m_sequence_number; }
|
2019-08-08 12:32:35 +10:00
|
|
|
u32 packets_in() const { return m_packets_in; }
|
|
|
|
u32 bytes_in() const { return m_bytes_in; }
|
|
|
|
u32 packets_out() const { return m_packets_out; }
|
|
|
|
u32 bytes_out() const { return m_bytes_out; }
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
void send_tcp_packet(u16 flags, const void* = nullptr, int = 0);
|
2019-08-08 12:32:35 +10:00
|
|
|
void record_incoming_data(int);
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2019-08-06 23:40:38 +10:00
|
|
|
static Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>& sockets_by_tuple();
|
|
|
|
static TCPSocketHandle from_tuple(const IPv4SocketTuple& tuple);
|
|
|
|
static TCPSocketHandle from_endpoints(const IPv4Address& local_address, u16 local_port, const IPv4Address& peer_address, u16 peer_port);
|
2019-03-14 12:28:30 +01:00
|
|
|
|
2019-03-14 12:20:38 +01:00
|
|
|
private:
|
|
|
|
explicit TCPSocket(int protocol);
|
2019-05-03 20:42:43 +02:00
|
|
|
virtual const char* class_name() const override { return "TCPSocket"; }
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
static NetworkOrdered<u16> compute_tcp_checksum(const IPv4Address& source, const IPv4Address& destination, const TCPPacket&, u16 payload_size);
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2019-08-04 21:07:50 +02:00
|
|
|
virtual int protocol_receive(const KBuffer&, void* buffer, size_t buffer_size, int flags) override;
|
2019-03-14 12:20:38 +01:00
|
|
|
virtual int protocol_send(const void*, int) override;
|
2019-06-07 09:36:51 +02:00
|
|
|
virtual KResult protocol_connect(FileDescription&, ShouldBlock) override;
|
2019-05-04 16:40:34 +02:00
|
|
|
virtual int protocol_allocate_local_port() override;
|
2019-03-14 15:23:32 +01:00
|
|
|
virtual bool protocol_is_disconnected() const override;
|
2019-05-03 21:51:40 +02:00
|
|
|
virtual KResult protocol_bind() override;
|
2019-08-06 23:40:38 +10:00
|
|
|
virtual KResult protocol_listen() override;
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2019-08-06 23:40:38 +10:00
|
|
|
NetworkAdapter* m_adapter { nullptr };
|
2019-07-03 21:17:35 +02:00
|
|
|
u32 m_sequence_number { 0 };
|
|
|
|
u32 m_ack_number { 0 };
|
2019-08-06 23:40:38 +10:00
|
|
|
State m_state { State::Closed };
|
2019-08-08 12:32:35 +10:00
|
|
|
u32 m_packets_in { 0 };
|
|
|
|
u32 m_bytes_in { 0 };
|
|
|
|
u32 m_packets_out { 0 };
|
|
|
|
u32 m_bytes_out { 0 };
|
2019-03-14 12:20:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class TCPSocketHandle : public SocketHandle {
|
|
|
|
public:
|
2019-05-28 11:53:16 +02:00
|
|
|
TCPSocketHandle() {}
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
TCPSocketHandle(RefPtr<TCPSocket>&& socket)
|
2019-03-14 12:20:38 +01:00
|
|
|
: SocketHandle(move(socket))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPSocketHandle(TCPSocketHandle&& other)
|
|
|
|
: SocketHandle(move(other))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPSocketHandle(const TCPSocketHandle&) = delete;
|
|
|
|
TCPSocketHandle& operator=(const TCPSocketHandle&) = delete;
|
|
|
|
|
|
|
|
TCPSocket* operator->() { return &socket(); }
|
|
|
|
const TCPSocket* operator->() const { return &socket(); }
|
|
|
|
|
|
|
|
TCPSocket& socket() { return static_cast<TCPSocket&>(SocketHandle::socket()); }
|
|
|
|
const TCPSocket& socket() const { return static_cast<const TCPSocket&>(SocketHandle::socket()); }
|
|
|
|
};
|