2019-03-12 17:51:42 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Kernel/Socket.h>
|
|
|
|
#include <Kernel/DoubleBuffer.h>
|
|
|
|
#include <Kernel/IPv4.h>
|
2019-03-13 18:05:56 +03:00
|
|
|
#include <AK/HashMap.h>
|
2019-03-12 19:27:07 +03:00
|
|
|
#include <AK/Lock.h>
|
|
|
|
#include <AK/SinglyLinkedList.h>
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2019-03-14 01:14:30 +03:00
|
|
|
class NetworkAdapter;
|
|
|
|
class TCPPacket;
|
|
|
|
|
|
|
|
enum TCPState {
|
|
|
|
Disconnected,
|
|
|
|
Connecting1,
|
|
|
|
Connecting2,
|
|
|
|
Connected,
|
|
|
|
Disconnecting1,
|
|
|
|
Disconnecting2,
|
|
|
|
};
|
|
|
|
|
2019-03-12 17:51:42 +03:00
|
|
|
class IPv4Socket final : public Socket {
|
|
|
|
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-13 18:05:56 +03:00
|
|
|
static Lockable<HashMap<word, IPv4Socket*>>& sockets_by_udp_port();
|
|
|
|
static Lockable<HashMap<word, IPv4Socket*>>& sockets_by_tcp_port();
|
2019-03-12 19:27:07 +03:00
|
|
|
|
2019-03-12 17:51:42 +03:00
|
|
|
virtual KResult bind(const sockaddr*, socklen_t) override;
|
|
|
|
virtual KResult connect(const sockaddr*, socklen_t) override;
|
|
|
|
virtual bool get_address(sockaddr*, socklen_t*) override;
|
|
|
|
virtual void attach_fd(SocketRole) override;
|
|
|
|
virtual void detach_fd(SocketRole) override;
|
|
|
|
virtual bool can_read(SocketRole) const override;
|
|
|
|
virtual ssize_t read(SocketRole, byte*, ssize_t) override;
|
|
|
|
virtual ssize_t write(SocketRole, const byte*, ssize_t) override;
|
|
|
|
virtual bool can_write(SocketRole) const override;
|
|
|
|
virtual ssize_t sendto(const void*, size_t, int, const sockaddr*, socklen_t) override;
|
2019-03-13 16:47:21 +03:00
|
|
|
virtual ssize_t recvfrom(void*, size_t, int flags, sockaddr*, socklen_t*) override;
|
2019-03-12 19:27:07 +03:00
|
|
|
|
|
|
|
void did_receive(ByteBuffer&&);
|
|
|
|
|
2019-03-13 16:22:27 +03:00
|
|
|
word source_port() const { return m_source_port; }
|
|
|
|
word destination_port() const { return m_destination_port; }
|
|
|
|
|
2019-03-14 01:14:30 +03:00
|
|
|
void send_tcp_packet(NetworkAdapter&, word flags, const void* payload = nullptr, size_t = 0);
|
|
|
|
void set_tcp_state(TCPState state) { m_tcp_state = state; }
|
|
|
|
TCPState tcp_state() const { return m_tcp_state; }
|
|
|
|
void set_tcp_ack_number(dword n) { m_tcp_ack_number = n; }
|
|
|
|
void set_tcp_sequence_number(dword n) { m_tcp_sequence_number = n; }
|
|
|
|
dword tcp_ack_number() const { return m_tcp_ack_number; }
|
|
|
|
dword tcp_sequence_number() const { return m_tcp_sequence_number; }
|
|
|
|
|
2019-03-12 17:51:42 +03:00
|
|
|
private:
|
|
|
|
IPv4Socket(int type, int protocol);
|
|
|
|
virtual bool is_ipv4() const override { return true; }
|
|
|
|
|
2019-03-13 18:05:56 +03:00
|
|
|
void allocate_source_port_if_needed();
|
2019-03-14 01:14:30 +03:00
|
|
|
NetworkOrdered<word> compute_tcp_checksum(const IPv4Address& source, const IPv4Address& destination, const TCPPacket&, word payload_size);
|
2019-03-13 18:05:56 +03:00
|
|
|
|
2019-03-12 17:51:42 +03:00
|
|
|
bool m_bound { false };
|
|
|
|
int m_attached_fds { 0 };
|
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 01:14:30 +03:00
|
|
|
dword m_tcp_sequence_number { 0 };
|
|
|
|
dword m_tcp_ack_number { 0 };
|
|
|
|
TCPState m_tcp_state { Disconnected };
|
|
|
|
|
2019-03-12 19:27:07 +03:00
|
|
|
bool m_can_read { false };
|
2019-03-12 17:51:42 +03:00
|
|
|
};
|
|
|
|
|