mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 18:08:12 +00:00
Kernel: Refactor TCP/IP stack
This has several significant changes to the networking stack. * Significant refactoring of the TCP state machine. Right now it's probably more fragile than it used to be, but handles quite a lot more of the handshake process. * `TCPSocket` holds a `NetworkAdapter*`, assigned during `connect()` or `bind()`, whichever comes first. * `listen()` is now virtual in `Socket` and intended to be implemented in its child classes * `listen()` no longer works without `bind()` - this is a bit of a regression, but listening sockets didn't work at all before, so it's not possible to observe the regression. * A file is exposed at `/proc/net_tcp`, which is a JSON document listing the current TCP sockets with a bit of metadata. * There's an `ETHERNET_VERY_DEBUG` flag for dumping packet's content out to `kprintf`. It is, indeed, _very debug_.
This commit is contained in:
parent
c973a51a23
commit
73c998dbfc
12 changed files with 446 additions and 84 deletions
|
@ -1,19 +1,58 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <Kernel/Net/IPv4Socket.h>
|
||||
|
||||
class TCPSocket final : public IPv4Socket {
|
||||
public:
|
||||
static void for_each(Function<void(TCPSocket*&)>);
|
||||
static NonnullRefPtr<TCPSocket> create(int protocol);
|
||||
virtual ~TCPSocket() override;
|
||||
|
||||
enum class State {
|
||||
Disconnected,
|
||||
Connecting,
|
||||
Connected,
|
||||
Disconnecting,
|
||||
Closed,
|
||||
Listen,
|
||||
SynSent,
|
||||
SynReceived,
|
||||
Established,
|
||||
CloseWait,
|
||||
LastAck,
|
||||
FinWait1,
|
||||
FinWait2,
|
||||
Closing,
|
||||
TimeWait,
|
||||
};
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
State state() const { return m_state; }
|
||||
void set_state(State state) { m_state = state; }
|
||||
|
||||
|
@ -24,8 +63,9 @@ public:
|
|||
|
||||
void send_tcp_packet(u16 flags, const void* = nullptr, int = 0);
|
||||
|
||||
static Lockable<HashMap<u16, TCPSocket*>>& sockets_by_port();
|
||||
static TCPSocketHandle from_port(u16);
|
||||
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);
|
||||
|
||||
private:
|
||||
explicit TCPSocket(int protocol);
|
||||
|
@ -39,10 +79,12 @@ private:
|
|||
virtual int protocol_allocate_local_port() override;
|
||||
virtual bool protocol_is_disconnected() const override;
|
||||
virtual KResult protocol_bind() override;
|
||||
virtual KResult protocol_listen() override;
|
||||
|
||||
NetworkAdapter* m_adapter { nullptr };
|
||||
u32 m_sequence_number { 0 };
|
||||
u32 m_ack_number { 0 };
|
||||
State m_state { State::Disconnected };
|
||||
State m_state { State::Closed };
|
||||
};
|
||||
|
||||
class TCPSocketHandle : public SocketHandle {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue